iOS實(shí)現(xiàn)UIButton的拖拽功能
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)UIButton拖拽功能的具體代碼,供大家參考,具體內(nèi)容如下
在APP界面中,把資訊等功能設(shè)置為懸浮的Button并且能夠讓用戶自己拖拽調(diào)整位置很常用。這里實(shí)現(xiàn)一下上述的功能,我們先看一下效果圖

這里給UIButton的拖拽的范圍進(jìn)行了設(shè)定,超過了這個(gè)區(qū)域則強(qiáng)行結(jié)束拖拽。
我們知道UIButton是自帶手勢事件的,但我們不選擇使其自帶的手勢事件來響應(yīng)拖拽,其原因?yàn)樽詭У氖謩莶缓玫玫胶涂刂仆献У臓顟B(tài)。我們創(chuàng)建一個(gè) 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];
? ??
}然后我們實(shí)現(xiàn) movingBtn
-(void)movingBtn:(UIPanGestureRecognizer *)recognizer{
? ? //得到拖拽的UIButton
? ? UIButton *button = (UIButton *)recognizer.view;
? ? //得到拖拽的偏移量
? ? CGPoint translation = [recognizer translationInView:button];
? ??
? ? //對(duì)拖拽事件的狀態(tài)進(jìn)行判斷 也就是選擇自定義手勢的原因
? ? if(recognizer.state == UIGestureRecognizerStateBegan){
? ? ? ??
? ? }else if(recognizer.state == UIGestureRecognizerStateChanged){
? ? ? ? //實(shí)時(shí)設(shè)置button的center、recognizer
? ? ? ? button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
? ? ? ? [recognizer setTranslation:CGPointZero inView:button];
?
? ? ? ? //對(duì)button的位置進(jìn)行判斷,超出范圍則強(qiáng)行使拖拽事件結(jié)束
? ? ? ? //這個(gè)范圍可以自己自定義
? ? ? ? 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){
? ? ? ? //得到結(jié)束時(shí)button的center
? ? ? ? //根據(jù)center 判斷應(yīng)該的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];
? ? }
}至此 我們就實(shí)現(xiàn)了可拖拽的UIButton
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼
本篇文章主要介紹了iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
iOS開發(fā)之TableView實(shí)現(xiàn)完整的分割線詳解
在iOS開發(fā)中, tableView是我們最常用的UI控件之一。所以這篇文章主要給大家詳細(xì)介紹了關(guān)于iOS中的TableView分割線,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
iOS實(shí)現(xiàn)底部彈出PopupWindow效果 iOS改變背景透明效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)底部彈出PopupWindow效果,iOS改變背景透明效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

