项目中为了语音功能的更好体验,加了个悬浮按钮,网上找了好久,最终集成了一个悬浮按钮,效果还不错。
参考Demo
悬浮按钮.gif
自定义个UIButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
//触摸-清扫
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
MoveEnabled = NO;
[super touchesBegan:touches withEvent:event];
if (!MoveEnable) {
return;
}
UITouch *touch = [touches anyObject];
beginpoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
MoveEnabled = YES;//单击事件可用
if (!MoveEnable) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
//偏移量
float offsetX = currentPosition.x - beginpoint.x;
float offsetY = currentPosition.y - beginpoint.y;
//移动后的中心坐标
self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
//x轴左右极限坐标
if (self.center.x > (self.superview.frame.size.width-self.frame.size.width/2)) {
CGFloat x = self.superview.frame.size.width-self.frame.size.width/2;
self.center = CGPointMake(x, self.center.y + offs