前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >创建一个可任意拖放的控件

创建一个可任意拖放的控件

作者头像
周希
发布2019-10-15 01:02:11
3610
发布2019-10-15 01:02:11
举报

看到有个app上面有个浮动的可以随意拖动的漂浮控件

想了下可以用UIButton, button设置要显示的图片, 然后通过UIPanGestureRecognizer来实现调整位置

上代码:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     
 4     //初始化button
 5     _button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
 6     _button.backgroundColor     = [UIColor redColor];
 7     _button.layer.cornerRadius  = 50;
 8     _button.layer.masksToBounds = YES;
 9     _button.center              = self.view.center;
10     
11     [self.view addSubview:_button];
12     
13     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
14     [self.button addGestureRecognizer:panGesture];
15 }
16 
17 - (void)handlePanGesture: (UIPanGestureRecognizer *)recognizer {
18     
19     CGPoint translation    = [recognizer translationInView:self.view];
20     recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
21                                          recognizer.view.center.y + translation.y);
22     [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
23 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档