查看: 232|回复: 0

[IOS开发教程] ios可拖动按钮实例

发表于 7 天前

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

话不多说,上代码:

  1. - (UIButton *)panButton {
  2. if (!_panButton) {
  3. UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  4. _panButton = [[UIButton alloc] init];
  5. _panButton.backgroundColor = [UIColor blueColor];
  6. _panButton.layer.borderWidth = 1.f;
  7. _panButton.layer.borderColor = [UIColor greenColor].CGColor;
  8. [_panButton setTitle:@"清除缓存" forState:UIControlStateNormal];
  9. _panButton.titleLabel.font = [UIFont systemFontOfSize:9];
  10. [_panButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  11. [_panButton addGestureRecognizer:panGesture];
  12. }
  13. return _panButton;
  14. }
  15. - (void)panAction:(UIPanGestureRecognizer *)recognizer {
  16. CGPoint translationPoint = [recognizer translationInView:self.view];
  17. CGPoint center = recognizer.view.center;
  18. recognizer.view.center = CGPointMake(center.x + translationPoint.x, center.y + translationPoint.y);
  19. [recognizer setTranslation:CGPointZero inView:self.view];
  20. }
  21. -(void)buttonAction:(UIButton *)sender
  22. {
  23. NSLog(@"烦人,点我干啥~");
  24. }
复制代码

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



回复

使用道具 举报