ios - 如何使 UIButton 可拖动但不触发点击事件

标签 ios uibutton

我一般都是用这个方法来实现UIButton的可移动;

[button addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];

但是它会同时触发touchUpInside事件,我需要touchUpInside事件做一些其他事情。

那么有人知道如何避免这种情况吗?

非常感谢;

使用下面这些代码来解决它;

  [button addTarget:self action:@selector(touchDown:withEvent:) forControlEvents:UIControlEventTouchDown];

    [button addTarget:self action:@selector(touchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [button addTarget:self action:@selector(touchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];

    - (void) touchUpInside :(UIControl*)c withEvent:ev{
    NSLog(@"touch Up inside");
    if (count) {
        NSLog(@"here I can do something about clicking event");
    }
}

    - (void) touchDown:(UIControl*)c withEvent:ev{
    NSLog(@"touch Down");
    count = YES;
}

    -(void) touchDragInside:(UIControl*)c withEvent:ev{
    NSLog(@"touch drag inside");
    c.center = [[[ev allTouches] anyObject] locationInView:self.view];
    count = NO;
}

最佳答案

实际上,实现此目的的最佳方法是使用 UIPanGestureRecognizer 并将其添加到按钮。它可以很好地处理拖动,您也可以点击它而无需执行任何其他操作。

关于ios - 如何使 UIButton 可拖动但不触发点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16118502/

相关文章:

ios - 当主应用程序未打开时,Swift 如何从今天的扩展中打开特定的 View Controller

objective-c - 调试间歇性卡住的 NSOperationQueue

ios - UIButton子类更改标题颜色,而无需使用setTitleColor(_,用于状态:) [Swift]

android - 访问短信移动应用程序

ios - (Swift) Firebase keepSynced(true) 不起作用

ios - 禁用 UIPickerView 中的特定行值

ios - 将 UIPopover 正确放置在动态按钮旁边

ios - 自定义形状可触摸区域

ios - 如何使用 FontAwesome 制作多行的 UIButton?

ios - UIButtonLabel 框架宽度错误