gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer

用iOS系统的MKMapView,想在用户双击地图时做一些操作,首先我想在下面的方法中处理:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{

YBLog(@"%s",__func__);

//UITouch  *touch =[touches anyObject];

//NSLog(@"touch.tapCount = %i",touch.tapCount);

//if(touch.tapCount ==2) //这里无法获取双击事件,因为双击事件被mapView拦截了,准确的说时被mapview内部的UITapGestureRecognizer手势拦截了

//{

//self.view.backgroundColor = [UIColor redColor];

//}

}

然后我又给mapview添加了一个UITapGestureRecognizer手势:如下:

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(invokeGesture:)];

tap.numberOfTapsRequired=2;

tap.delegate=self;

[self  addGestureRecognizer:tap];

但是发现在双击的时候根本没有调用invokeGesture:方法,为什么会这样呢?

原因是:

MKMapView内部实现时,已添加了1个UITapGestureRecognizer,而这里我们又添加了另外1个UITapGestureRecognizer,也就是说同1个MKMapView有两个相同类型的手势辨认,但是运行时内部默许相同类型的手势辨认只有1个会得到处理,所以第1段代码始终没有输出invokeGesture:。幸亏UIPanGestureRecognizerDelegate提供了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer方法,该方法返回YES时,意味着所有相同类型的手势辨认都会得到处理。

所以解决方法如下:

//该方法返回YES时,意味着所有相同类型的手势都会得到处理。

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer

shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{

returnYES;

}

你可能感兴趣的:(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer)