網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了iOS實現(xiàn)UIButton拖拽功能的具體代碼,供大家參考,具體內(nèi)容如下
在APP界面中,把資訊等功能設置為懸浮的Button并且能夠讓用戶自己拖拽調(diào)整位置很常用。這里實現(xiàn)一下上述的功能,我們先看一下效果圖
這里給UIButton的拖拽的范圍進行了設定,超過了這個區(qū)域則強行結束拖拽。
我們知道UIButton是自帶手勢事件的,但我們不選擇使其自帶的手勢事件來響應拖拽,其原因為自帶的手勢不好得到和控制拖拽的狀態(tài)。我們創(chuàng)建一個 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];
? ??
}
然后我們實現(xiàn) movingBtn
-(void)movingBtn:(UIPanGestureRecognizer *)recognizer{
? ? //得到拖拽的UIButton
? ? UIButton *button = (UIButton *)recognizer.view;
? ? //得到拖拽的偏移量
? ? CGPoint translation = [recognizer translationInView:button];
? ??
? ? //對拖拽事件的狀態(tài)進行判斷 也就是選擇自定義手勢的原因
? ? 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
? ? ? ? //根據(jù)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];
? ? }
}
至此 我們就實現(xiàn)了可拖拽的UIButton
原文鏈接:https://blog.csdn.net/XXJcanbethebest/article/details/122592725
相關推薦
- 2023-08-30 Git忽略已經(jīng)提交過一次文件Git忽略文件
- 2022-06-01 c++?深入理解歸并排序的用法_C 語言
- 2023-03-28 python?list與numpy數(shù)組效率對比_python
- 2022-10-29 .Net?Core?配置文件讀取IOptions,IOptionsMonitor,IOptionsS
- 2022-09-09 PyTorch策略梯度算法詳情_python
- 2022-07-04 PyG搭建GCN模型實現(xiàn)節(jié)點分類GCNConv參數(shù)詳解_python
- 2022-12-07 C語言內(nèi)存分布與heap空間分別詳細講解_C 語言
- 2023-07-10 Bean的生命周期和作用域
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支