我有一个按钮,行为在一种方式,当用户点击它和另一个当用户双击它。在用户双击按钮的情况下,我不希望发生单击行为。
在识别双击的情况下,如何防止调用触碰事件?
发布于 2015-05-27 12:36:04
你可以使用Target-action;对于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秒的延迟!
发布于 2015-05-27 12:05:47
据我所知,你想在同一个按钮上有不同的行为,所以只需在下面的代码中应用两个不同的tap gesture.The可能会对你有所帮助。
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];https://stackoverflow.com/questions/30472494
复制相似问题