首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIButton -在调用触摸重复时防止调用触碰事件

UIButton -在调用触摸重复时防止调用触碰事件
EN

Stack Overflow用户
提问于 2015-05-27 11:12:45
回答 2查看 875关注 0票数 0

我有一个按钮,行为在一种方式,当用户点击它和另一个当用户双击它。在用户双击按钮的情况下,我不希望发生单击行为。

在识别双击的情况下,如何防止调用触碰事件?

EN

回答 2

Stack Overflow用户

发布于 2015-05-27 12:36:04

你可以使用Target-action;对于UIControlEvents,你可以像这样使用"UIControlEventTouchDown“和"UIControlEventTouchDownRepeat”:

代码语言:javascript
复制
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秒的延迟!

票数 1
EN

Stack Overflow用户

发布于 2015-05-27 12:05:47

据我所知,你想在同一个按钮上有不同的行为,所以只需在下面的代码中应用两个不同的tap gesture.The可能会对你有所帮助。

代码语言:javascript
复制
UIButton *btn1=[[UIButton alloc]init];  //your button

//Two diff method call for two diff behaviour

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(singleTapEvent:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(doubleTapEvent:)];

//specify the number of tapping event require to execute code.
singleTap.numberOfTapsRequired = 1;  
doubleTap.numberOfTapsRequired = 2;

[singleTap requireGestureRecognizerToFail:DoubleTap];

//Apply  multiple tap gesture to your  button
[btn1 addGestureRecognizer:singleTap]; 
[btn1 addGestureRecognizer:doubleTap];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30472494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档