最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實(shí)現(xiàn)UIButton的拖拽功能

 更新時(shí)間:2022年08月05日 14:43:05   作者:XXJcanbethebest  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)UIButton的拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評(píng)論

呼图壁县| 西青区| 从江县| 柏乡县| 天长市| 新宾| 日照市| 项城市| 鲜城| 鄯善县| 仁寿县| 通道| 鸡西市| 余庆县| 高清| 阿巴嘎旗| 英德市| 德钦县| 桦甸市| 巴南区| 海安县| 长汀县| 红安县| 交口县| 商丘市| 泰安市| 兰考县| 松溪县| 老河口市| 天峨县| 鹿邑县| 扬州市| 奈曼旗| 嘉义市| 延安市| 定结县| 大理市| 辰溪县| 祁阳县| 喀喇沁旗| 四平市|