網站首頁 編程語言 正文
本文實例為大家分享了iOS實現UIButton拖拽功能的具體代碼,供大家參考,具體內容如下
在APP界面中,把資訊等功能設置為懸浮的Button并且能夠讓用戶自己拖拽調整位置很常用。這里實現一下上述的功能,我們先看一下效果圖
這里給UIButton的拖拽的范圍進行了設定,超過了這個區域則強行結束拖拽。
我們知道UIButton是自帶手勢事件的,但我們不選擇使其自帶的手勢事件來響應拖拽,其原因為自帶的手勢不好得到和控制拖拽的狀態。我們創建一個 UIPanGestureRecognizer 添加給UIButton
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? Width=[UIScreen mainScreen].bounds.size.width;
? ? Height=[UIScreen mainScreen].bounds.size.height;
? ??
? ? UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 60, 60)];
? ? btn.backgroundColor=[UIColor blueColor];
? ? [btn setTitle:@"B" forState:UIControlStateNormal];
? ? //UIPanGestureRecognizer手勢
? ? UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(movingBtn:)];
? ? [btn addGestureRecognizer:pan];
? ??
? ? [self.view addSubview:btn];
? ??
}
然后我們實現 movingBtn
-(void)movingBtn:(UIPanGestureRecognizer *)recognizer{
? ? //得到拖拽的UIButton
? ? UIButton *button = (UIButton *)recognizer.view;
? ? //得到拖拽的偏移量
? ? CGPoint translation = [recognizer translationInView:button];
? ??
? ? //對拖拽事件的狀態進行判斷 也就是選擇自定義手勢的原因
? ? if(recognizer.state == UIGestureRecognizerStateBegan){
? ? ? ??
? ? }else if(recognizer.state == UIGestureRecognizerStateChanged){
? ? ? ? //實時設置button的center、recognizer
? ? ? ? button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
? ? ? ? [recognizer setTranslation:CGPointZero inView:button];
?
? ? ? ? //對button的位置進行判斷,超出范圍則強行使拖拽事件結束
? ? ? ? //這個范圍可以自己自定義
? ? ? ? if(button.center.x <= 40 || button.center.x>=Width-40 || button.center.y <=100 || button.center.y >=Height-100){
? ? ? ? ? ? [recognizer setState:UIGestureRecognizerStateEnded];
? ? ? ? }
? ? ? ??
? ? }else if(recognizer.state == UIGestureRecognizerStateEnded){
? ? ? ? //得到結束時button的center
? ? ? ? //根據center 判斷應該的X和Y
? ? ? ? CGFloat newX=button.center.x;
? ? ? ? if(button.center.x <=Width/2) newX=40;
? ? ? ? else if(button.center.x >=Width/2) newX=Width-40;
? ? ? ??
? ? ? ? CGFloat newY=button.center.y;
? ? ? ? if(button.center.y <=100) newY=100;
? ? ? ? else if(button.center.y >=Height-100) newY=Height-100;
? ? ? ??
? ? ? ? button.center = CGPointMake(newX, newY);
? ? ? ? [recognizer setTranslation:CGPointZero inView:button];
? ? }
}
至此 我們就實現了可拖拽的UIButton
原文鏈接:https://blog.csdn.net/XXJcanbethebest/article/details/122592725
相關推薦
- 2022-07-16 搭建一個Electron跨平臺桌面應用程序
- 2022-05-23 Redis監控工具RedisInsight安裝與使用_Redis
- 2022-09-15 C語言編寫實現學生管理系統_C 語言
- 2022-08-14 python?中的@property的用法詳解_python
- 2022-08-04 Go?slice切片make生成append追加copy復制示例_Golang
- 2022-12-10 C++模擬實現string的方法詳解_C 語言
- 2022-09-20 Go?Redis客戶端使用的兩種對比_Golang
- 2022-10-31 Android數據緩存框架內置ORM功能使用教程_Android
- 最近更新
-
- 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同步修改后的遠程分支