SoFunction
Updated on 2025-03-04

ios draggable button example

Recently, the product is in trouble and I want to make the small customer service button of Xuxian.com. Although it is not difficult, I am lazy. Haha, I made one on the blog, but the click event and drag are repeated. Let's just write one for reference only.

Without further ado, please enter the code:

- (UIButton *)panButton {
 if (!_panButton) {
  UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  _panButton = [[UIButton alloc] init];
  _panButton.backgroundColor = [UIColor blueColor];
  _panButton. = ;
  _panButton. = [UIColor greenColor].CGColor;
  [_panButton setTitle:@"Clear cache" forState:UIControlStateNormal];
  _panButton. = [UIFont systemFontOfSize:9];
  [_panButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  [_panButton addGestureRecognizer:panGesture];
 }
 return _panButton;
}
- (void)panAction:(UIPanGestureRecognizer *)recognizer {
 CGPoint translationPoint = [recognizer translationInView:];
 CGPoint center = ;
  = CGPointMake( + ,  + );
 [recognizer setTranslation:CGPointZero inView:];
}
-(void)buttonAction:(UIButton *)sender
{
 NSLog(@"Annoying, why should I do~");
}

The above example of the ios draggable button is all the content I share with you. I hope you can give you a reference and I hope you can support me more.