self.doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; self.doubleTap.numberOfTapsRequired = 2; self.doubleTap.numberOfTouchesRequired = 1; [mapView_ addGestureRecognizer:doubleTap_]; self.singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; self.singleTap.numberOfTapsRequired = 1; self.singleTap.numberOfTouchesRequired = 1; [self.singleTap requireGestureRecognizerToFail: doubleTap_]; [mapView_ addGestureRecognizer:singleTap_];
现在,这引起苹果公司的注意并不奇怪:
Note: In the case of the single-tap versus double-tap gestures,if a
single-tap gesture recognizer doesn’t require the double-tap
recognizer to fail,you should expect to receive your single-tap
actions before your double-tap actions,even in the case of a double
tap. This is expected and desirable behavior because the best user
experience generally involves stackable actions.
所以我将requireGestureRecognizerToFail添加到我的单击识别器中.
[singleTap requireGestureRecognizerToFail: doubleTap];
这确保我的单击识别器不会获得双击.
但…
现在我的双击识别器获得了双击,MKMapView无法获得它们.我已经尝试在识别器中将cancelsTouchesInView设置为NO,但这也没有帮助.
所以我需要一种方法来阻止我的单击识别器获得双击(这似乎不太可能)或者让我的双击事件到我的mapView.
解决方法
双击由地图视图自动处理,不会干扰单击.
在iOS 5.x中,要使地图视图执行其正常的双击处理而不干扰您的单击,请实现shouldRecognizeSimultaneouslyWithGestureRecognizer委托方法并从中返回YES(需要设置双击手势的委托属性)仅识别器).
不确定为什么在iOS 6中没有必要这样做.