用iOS系統(tǒng)的MKMapView,想在用戶雙擊地圖時(shí)做一些操作,首先我想在下面的方法中處理:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
YBLog(@"%s",__func__);
//UITouch ?*touch =[touches anyObject];
//NSLog(@"touch.tapCount = %i",touch.tapCount);
//if(touch.tapCount ==2) //這里無法獲取雙擊事件哪痰,因?yàn)殡p擊事件被mapView攔截了,準(zhǔn)確的說時(shí)被mapview內(nèi)部的UITapGestureRecognizer手勢(shì)攔截了
//{
//self.view.backgroundColor = [UIColor redColor];
//}
}
然后我又給mapview添加了一個(gè)UITapGestureRecognizer手勢(shì):如下:
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self ?action:@selector(invokeGesture:)];
tap.numberOfTapsRequired=2;
tap.delegate=self;
[self ?addGestureRecognizer:tap];
但是發(fā)現(xiàn)在雙擊的時(shí)候根本沒有調(diào)用invokeGesture:方法,為什么會(huì)這樣呢廓八?
原因是:
MKMapView內(nèi)部實(shí)現(xiàn)時(shí)缤灵,已添加了1個(gè)UITapGestureRecognizer怠蹂,而這里我們又添加了另外1個(gè)UITapGestureRecognizer虐唠,也就是說同1個(gè)MKMapView有兩個(gè)相同類型的手勢(shì)辨認(rèn)搀愧,但是運(yùn)行時(shí)內(nèi)部默許相同類型的手勢(shì)辨認(rèn)只有1個(gè)會(huì)得到處理,所以第1段代碼始終沒有輸出invokeGesture:疆偿。幸虧UIPanGestureRecognizerDelegate提供了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer方法咱筛,該方法返回YES時(shí),意味著所有相同類型的手勢(shì)辨認(rèn)都會(huì)得到處理杆故。
所以解決方法如下:
//該方法返回YES時(shí)迅箩,意味著所有相同類型的手勢(shì)都會(huì)得到處理。
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{
returnYES;
}