您当前的位置: 首页 > 技术文章 > 移动开发

游戏联运系统SDK iOS悬浮球的实现方法

作者: 时间:2023-09-30阅读数:人阅读

本文将为大家实现在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悬浮球的实现方法。如果有更好的实现方法也希望大家多多评论交流。

本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:licqi@yunshuaiweb.com

标签: ios objective-c
加载中~
如果您对我们的成果表示认同并且觉得对你有所帮助可以给我们捐赠。您的帮助是对我们最大的支持和动力!
捐赠我们
扫码支持 扫码支持
扫码捐赠,你说多少就多少
2
5
10
20
50
自定义
您当前余额:元
支付宝
微信
余额

打开支付宝扫一扫,即可进行扫码捐赠哦

打开微信扫一扫,即可进行扫码捐赠哦

打开QQ钱包扫一扫,即可进行扫码捐赠哦