ios可拖动按钮实例

最近产品抽风,想做许鲜网的那个小客服按钮,虽然没啥难度,但是我懒啊,哈哈,上度娘搞了一个,但是点击事件和拖动重复了,擦。干脆写一个吧,仅供参考。

话不多说,上代码:

- (UIButton *)panButton {
 if (!_panButton) {
  UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  _panButton = [[UIButton alloc] init];
  _panButton.backgroundColor = [UIColor blueColor];
  _panButton.layer.borderWidth = 1.f;
  _panButton.layer.borderColor = [UIColor greenColor].CGColor;
  [_panButton setTitle:@"清除缓存" forState:UIControlStateNormal];
  _panButton.titleLabel.font = [UIFont systemFontOfSize:9];
  [_panButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  [_panButton addGestureRecognizer:panGesture];
 }
 return _panButton;
}
- (void)panAction:(UIPanGestureRecognizer *)recognizer {
 CGPoint translationPoint = [recognizer translationInView:self.view];
 CGPoint center = recognizer.view.center;
 recognizer.view.center = CGPointMake(center.x + translationPoint.x, center.y + translationPoint.y);
 [recognizer setTranslation:CGPointZero inView:self.view];
}
-(void)buttonAction:(UIButton *)sender
{
 NSLog(@"烦人,点我干啥~");
}

以上这篇ios可拖动按钮实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。