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

news/2024/11/24 21:08:58/

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


http://www.ppmy.cn/news/877736.html

相关文章

过山车大亨3《RollerCoaster Tycoon Complete Edition》 Mac版

关于这款游戏 你的游乐园&#xff0c;一切由你作主 不论你想打造成什么样貌&#xff0c;《RollerCoaster Tycoon 3》都提供充分的工具和自由&#xff0c;让你能尽情建造出梦想中的游乐园。 像企业大亨般管理游乐园&#xff0c;一路攀上颠峰 控管游乐园的财务、商店、服务和…

超级悬浮球,Plus版

前言 说到悬浮球大家可能瞬间就会想到X60、X毒霸、X管家…… 虽说这几家整体上都有些流氓&#xff0c;但悬浮球这个小功能还是非常好用的 今天给大家带来一款开源的超级悬浮球&#xff0c;拥有上述三大应用及火绒的悬浮球样式 还集成了网速显示、壁纸切换、文本翻译、文字识…

过山车大亨1android,过山车大亨最新版

过山车大亨最新版是一款非常好玩的趣味模拟经营游戏。在这款游戏中玩家将要去建造一家属于自己的游乐场来吸引更多的顾客&#xff0c;令人兴奋的平面玩乐景点&#xff0c;积极的游客&#xff0c;用户自定义的内容&#xff0c;强大的社交功能以及更多 – 尽在这款大型娱乐游戏之…

过山车大亨3拉拉地通关要诀【亲测可用】

整个过山车大亨3就只有拉拉地可能卡关&#xff0c;即使使用秘籍也难以通关。 接下来就是攻略要诀&#xff1a; 第一步&#xff1a;基本没问题 记得开局卖掉观光塔&#xff0c;否则会卡很多人。 第二步&#xff1a;vip经过烟花的时候手动点开烟花编辑器&#xff0c;然后手动播…

接口突然超时10宗罪。。。

前言 不知道你有没有遇到过这样的场景&#xff1a;我们提供的某个API接口&#xff0c;响应时间原本一直都很快&#xff0c;但在某个不经意的时间点&#xff0c;突然出现了接口超时。 也许你会有点懵&#xff0c;到底是为什么呢&#xff1f; 今天跟大家一起聊聊接口突然超时的…

【论文笔记】SINE: SINgle Image Editing with Text-to-Image Diffusion Models

声明 不定期更新自己精度论文&#xff0c;通俗易懂&#xff0c;初级小白也可以理解 涉及范围&#xff1a;深度学习方向&#xff0c;包括 CV、NLP 论文标题&#xff1a;SINE: SINgle Image Editing with Text-to-Image Diffusion Models 论文链接&#xff1a;https://www.seman…

通过LWIP 的RAW API 进行与服务器的UDP通信(stm32 f103 -- enc28j60)

LWIP的RAW API UDP通信详解(stm32f103—enc28j60) 项目整体托管到github上 实现任务 通过接收到上位机端发送来的数据来实现控制开发板做相应的操作。 代码实现 首先看看几个主要的结构体 //发送数据包 struct sardata {u8 head[4]; //标志头u16 length; //长度u8 hum…

html j红绿灯转换倒计时的代码,74ls160应用电路图大全(数字钟\TLC320AD75C\交通灯\LM324)...

描述 74ls160应用电路图(一) 数字钟是计数电路的一种典型应用,其构成原理框图如下图所示。它主要由三部分组成: (1)秒脉冲发生电路 它由32768Hz的石英晶体振荡器和若干级分频电路构成,振荡器产生32768Hz的方波,由于使用了晶体,振荡频率准确且稳定,经过216=65536分频后,再…