我有两个UITapGestureRecognizer
:singleTap
和doubleTap
初始化了两个不同的行为。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[singleTap requireGestureRecognizerToFail:doubleTap];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:singleTap];
当我在模拟器中运行我的应用程序时,单击可以正确响应但不是双击!当我双击无意义时,我认为iOS剂量识别双击,因为单击的动作没有被调用(由于[singleTap requireGestureRecognizerToFail:doubleTap];
),但我不明白为什么它不做动作handleDoubleTap
。
答案 0 :(得分:6)
我认为问题是UIImageView
和UILabel
都会覆盖YES
属性的默认值userInteractionEnabled
,并将其设置为NO
。
添加imageView.userInteractionEnabled = YES;
然后重试。
答案 1 :(得分:5)
以下代码适用于我:
- (void)handleTap:(UIGestureRecognizer*)gr {
NSLog(@"----------------- tap ----------------");
}
- (void)handleDoubleTap:(UIGestureRecognizer*)gr {
NSLog(@"================= double tap ============");
}
- (XXXView*)createXXXView {
XXXView *view = [[[XXXView alloc] init] autorelease];
view.xxx=...;//irrelevant
UITapGestureRecognizer *dtr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
dtr.numberOfTapsRequired = 2;
UIGestureRecognizer *tr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tr requireGestureRecognizerToFail:dtr];
[view addGestureRecognizer:tr];
[view addGestureRecognizer:dtr];
return view;
}