看到有个app上面有个浮动的可以随意拖动的漂浮控件
想了下可以用UIButton, button设置要显示的图片, 然后通过UIPanGestureRecognizer来实现调整位置
上代码:
代码语言:javascript复制 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 }