ios - UIButton - 当调用触摸重复时防止调用触摸事件

标签 ios cocoa-touch ios8 uibutton

我有一个按钮,当用户点击它时,它以一种方式运行,当用户双击它时,它以另一种方式运行。如果用户双击按钮,我不希望发生单击行为。

如果双击被识别,我如何阻止调用触地事件?

最佳答案

您可以使用目标操作;对于 UIControlEvents,您可以使用“UIControlEventTouchDown”和“UIControlEventTouchDownRepeat”,如下所示:

UIButton * button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(150, 200, 50, 50);
[button addTarget:self action:@selector(buttonSingleTap:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonMutipleTap:) forControlEvents:UIControlEventTouchDownRepeat];
[self.view addSubview:button];

- (void)buttonSingleTap:(UIButton *)btn{
    [self performSelector:@selector(buttonAction:) withObject:btn afterDelay:0.5];
}

- (void)buttonAction:(UIButton *)sender{
    NSLog(@"single tap");
}

- (void)buttonMutipleTap:(UIButton *)btn{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(buttonAction:) object:btn];
    NSLog(@"mutiple tap!");
}

但是会有0.5秒的延迟!

关于ios - UIButton - 当调用触摸重复时防止调用触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472494/

相关文章:

ios - 让当前用户删除一个 firebase child

ios - 自定义 UIViewController 中的内存管理

ios - 如何检测所有手指何时离开屏幕

cocoa-touch - NSDate 作为 NSDictionary 的键

iphone - 自定义 MPMoviePlayerController

swift - 在 Swift 中触摸任意位置时关闭 iOS 中的键盘

ios8 - iOS 自定义键盘自动布局

ios - `scene(_ scene: UIScene, continue userActivity: NSUserActivity)` 用户点击通用链接后启动应用时不被调用

ios - 这是什么 iOS 组件? (看图片)

Xcode 10 上的 iOS 模拟器崩溃