網站首頁 編程語言 正文
本文實例為大家分享了iOS實現全局懸浮按鈕的具體代碼,供大家參考,具體內容如下
現在有很多app都做這個全局按鈕
如上面兩張圖的效果,完成一個全局懸浮的按鈕,而且不會劃出屏幕外
既然是全局,那寫在AppDelegate中
UIWindow是一種特殊的UIView,它相當于一塊畫框,而UIView相當于里面的畫布。通常在一個app中只會有一個UIWindow。
AppDelegate.h
@interface AppDelegate : UIResponder@property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIButton *button; @end
AppDelegate.m
先button懶加載
- (UIButton*)button { ? ? if (!_button) { ? ? ? ? _button = [UIButton buttonWithType:UIButtonTypeCustom]; ? ? ? ? _button.frame = CGRectMake(258, 450, 60, 60);//初始在屏幕上的位置 ? ? ? ? [_button setImage:[UIImage imageNamed:@"bcl_btn_whole"] forState:UIControlStateNormal]; ? ? } ? ? return _button; }
然后將其加在window上,設置手勢
-(void)createButton{ ? ? if (!_button) { ? ? ? ? _window = [[UIApplication sharedApplication] keyWindow]; ? ? ? ? _window.backgroundColor = [UIColor whiteColor]; ? ? ? ? [_window addSubview:self.button]; ? ? ? ? UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self action:@selector(locationChange:)]; ? ? ? ? pan.delaysTouchesBegan = YES; ? ? ? ? [_button addGestureRecognizer:pan]; ? ? } }
這個呢是為了開機啟動兩秒后創建全局button
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ? ? [self performSelector:@selector(createButton) withObject:nil afterDelay:2]; }
最關鍵的就是設置button不要劃出屏幕外
以下四個else if分別為屏幕的上下左右
設置一個標記值isOVer
如果超出屏幕范圍,糾正回來
-(void)locationChange:(UIPanGestureRecognizer*)p{ ? ? CGFloat HEIGHT=_button.frame.size.height; ? ? CGFloat WIDTH=_button.frame.size.width; ? ? BOOL isOver = NO; ? ? CGPoint panPoint = [p locationInView:[UIApplication sharedApplication].windows[0]]; ? ? CGRect frame = CGRectMake(panPoint.x, panPoint.y, HEIGHT, WIDTH); ? ? NSLog(@"%f--panPoint.x-%f-panPoint.y-", panPoint.x, panPoint.y); ? ? if(p.state == UIGestureRecognizerStateChanged){ ? ? ? ? _button.center = CGPointMake(panPoint.x, panPoint.y); ? ? } ? ? else if(p.state == UIGestureRecognizerStateEnded){ ? ? ? ? if (panPoint.x + WIDTH > KScreenWidth) { ? ? ? ? ? ? frame.origin.x = KScreenWidth - WIDTH; ? ? ? ? ? ? isOver = YES; ? ? ? ? } else if (panPoint.y + HEIGHT > KScreenHeight) { ? ? ? ? ? ? frame.origin.y = KScreenHeight - HEIGHT; ? ? ? ? ? ? isOver = YES; ? ? ? ? } else if(panPoint.x - WIDTH / 2< 0) { ? ? ? ? ? ? frame.origin.x = 0; ? ? ? ? ? ? isOver = YES; ? ? ? ? } else if(panPoint.y - HEIGHT / 2 < 0) { ? ? ? ? ? ? frame.origin.y = 0; ? ? ? ? ? ? isOver = YES; ? ? ? ? } ? ? ? ? if (isOver) { ? ? ? ? ? ? [UIView animateWithDuration:0.3 animations:^{ ? ? ? ? ? ? ? ? self.button.frame = frame; ? ? ? ? ? ? }]; ? ? ? ? }
原文鏈接:https://blog.csdn.net/qiangshuting/article/details/87904469
相關推薦
- 2022-11-09 Python?Pandas讀寫txt和csv文件的方法詳解_python
- 2023-12-08 Can‘t open the append-only file: Permission denied
- 2022-06-18 Asp.Net?Core配置多環境log4net配置文件的全過程_實用技巧
- 2022-11-18 C++11正則表達式詳解(regex_match、regex_search和regex_replac
- 2022-08-18 詳解如何從Matlab中導出清晰的結果圖片_C 語言
- 2022-12-05 一文深入了解Python中的繼承知識點_python
- 2022-03-14 Linux磁盤格式化和掛載(linux服務器硬盤掛載步驟)
- 2023-06-05 Python利用GDAL模塊實現讀取柵格數據并對指定數據加以篩選掩膜_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支