我将singTap和doubleTap添加到如下代码的视图中:
-(void)awakeFromNib{ [self setUserInteractionEnabled:YES]; // UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)]; doubleTapGesture.numberOfTapsrequired = 2; [self addGestureRecognizer:doubleTapGesture]; UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)]; singleTapGesture.numberOfTapsrequired = 1; [self addGestureRecognizer:singleTapGesture]; } -(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTapGesture{ [[self delegate] singleTapOnView]; } -(void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTapGesture{ [[self delegate] doubleTapOnView]; }
当doubleTap时,singleTap也会开火.如何在doubleTap时禁用singleTap?谢谢!
解决方法
告诉单击识别器它需要双击识别器在触发之前失败:
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
(这实际上是UIGestureRecognizer docs for this method中的典型示例.)