本文将为大家实现在iOS中悬浮球功能,希望大家阅读完这篇文章后对相关知识有一定的了解。
首先我们创建一个View,在View内新建一个Button作为悬浮按钮,当然你也可以直接继承自UIButton。
这里添加了屏幕旋转监听,以便于做横竖屏适配,kWidthScan_HWan为竖屏时屏宽比、kWidthScan_HWan为横屏时屏宽比。
/// 控件初始化
- (void)initUI {[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];self.frame = CGRectMake(-22*kWidthScan_HWan, screenHeight_HWan/2, 44*kWidthScan_HWan, 44*kWidthScan_HWan);self.floatButton = [UIButton buttonWithType:UIButtonTypeCustom];[self.floatButton setBackgroundImage:[UIImage imageNamed:@"HWan94SDK.bundle/floatViewImg"] forState:UIControlStateNormal];[self addSubview:self.floatButton];UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragAction:)];[self addGestureRecognizer:panGesture];
}- (void)initFrame {[self.floatButton mas_makeConstraints:^(HWanMasConstraintMaker *make) {make.left.right.top.bottom.mas_equalTo(0);}];
}
悬浮球拖动主要代码如下,如果滑动悬浮球超过屏幕一半就停靠右侧,反之停靠左侧 ,这里竖屏情况下只允许停靠左右两侧,横屏下只允许停靠在充电口一侧,防止用户拖动屏幕会滑到通知栏或home键
/// 拖动悬浮球手势动作
- (void)dragAction:(UIPanGestureRecognizer *)gesture {UIGestureRecognizerState moveState = gesture.state;switch (moveState) {case UIGestureRecognizerStateBegan:break;case UIGestureRecognizerStateChanged:{CGPoint point = [gesture translationInView:[HWanBasicUniversal keyWindow]];self.center = CGPointMake(point.x + self.center.x, point.y + self.center.y);}break;case UIGestureRecognizerStateEnded:{CGPoint point = [gesture translationInView:[HWanBasicUniversal keyWindow]];CGPoint newPoint = CGPointMake(point.x + self.center.x, point.y + self.center.y);UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];if (newPoint.x < screenWidth_HWan/2) {newPoint.x = self.bounds.size.width/2;}else {newPoint.x = screenWidth_HWan - self.bounds.size.width/2;}if (newPoint.y <= self.bounds.size.height/2 +44){newPoint.y = self.bounds.size.height/2 + 44;} else if (newPoint.y >= screenHeight_HWan - self.bounds.size.height) {newPoint.y = screenHeight_HWan - self.bounds.size.height/2 - 44;}/// 0.5秒侧边吸附动画[UIView animateWithDuration:0.5 animations:^{if (interfaceOritation == UIInterfaceOrientationPortrait) {self.center = newPoint;} else if (interfaceOritation == UIInterfaceOrientationLandscapeRight) {self.center = CGPointMake(screenWidth_HWan - 22*kHeightScan_HWan, newPoint.y);} else if (interfaceOritation == UIInterfaceOrientationLandscapeLeft) {self.center = CGPointMake(22*kHeightScan_HWan, newPoint.y);}}];/// 延迟2秒隐藏一半悬浮球dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{if (interfaceOritation == UIInterfaceOrientationPortrait) {if (newPoint.x < screenWidth_HWan/2) {self.center = CGPointMake(newPoint.x-22*kWidthScan_HWan, newPoint.y);} else {self.center = CGPointMake(newPoint.x+22*kWidthScan_HWan, newPoint.y);}} else if (interfaceOritation == UIInterfaceOrientationLandscapeRight) {self.center = CGPointMake(screenWidth_HWan, newPoint.y);} else if (interfaceOritation == UIInterfaceOrientationLandscapeLeft) {self.center = CGPointMake(0, newPoint.y);}});}break;default:break;}[gesture setTranslation:CGPointZero inView:[HWanBasicUniversal keyWindow]];
}
切换横竖屏调整悬浮球位置
- (void)orientationChange:(NSNotification *)notification {UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];[self changeInterface:interfaceOritation];
}- (void)changeInterface:(UIInterfaceOrientation)oritation {if (oritation == UIInterfaceOrientationPortrait) {self.frame = CGRectMake(-22*kWidthScan_HWan, screenHeight_HWan/2, 44*kWidthScan_HWan, 44*kWidthScan_HWan);} else if (oritation == UIInterfaceOrientationLandscapeRight) {self.frame = CGRectMake(screenWidth_HWan - 22*kHeightScan_HWan, screenHeight_HWan/2, 44*kHeightScan_HWan, 44*kHeightScan_HWan);} else if (oritation == UIInterfaceOrientationLandscapeLeft) {self.frame = CGRectMake(-22*kHeightScan_HWan, screenHeight_HWan/2, 44*kHeightScan_HWan, 44*kHeightScan_HWan);}
}
以上就是iOS悬浮球的实现方法。如果有更好的实现方法也希望大家多多评论交流。