我有我在哪裏執行以下操作註釋的地圖:檢查是否用戶已經選擇了另一個註釋在地圖
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{
[self methodA];}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
[self methodB];}
兩種方法A和B刪除或mapView.superview添加視圖。
當處理一個註釋時,所有的工作都很好。 問題是當我有多個註釋,並且我選擇了兩個,一個接一個的時候,另一個是。
如果我點擊地圖中的任何其他地方,一切正常。但是當我在第一個註釋視圖之後點擊第二個註釋視圖時,它執行「didDeselectAnnotationView」,然後是「didSelectAnnotationView」,它同時調用方法A和B,但這不是我想要的。我希望它能檢測到我點擊了另一個註釋並忽略了這兩種方法。
我已經研究過這個,但還沒有找到任何解決方案。
我嘗試添加一個全局變量並使用它,以及識別用戶觸摸:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
// Get the specific point that was touched
CGPoint point = [touch locationInView:self.mapView];
for (id<MKAnnotation> annotation in self.mapView.annotations) {
MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation];
if (anView){
CGRect frame = CGRectMake(anView.frame.origin.x-5, anView.frame.origin.y-5, anView.frame.size.width+5, anView.frame.size.height+5);
if (CGRectContainsPoint(frame, point)) {
NSLog(@"BELONGS");
}
}
}}
然而,這並沒有捕捉到所有的接觸,再加上它是一個有點意大利麪條解決方案。
任何想法如何解決這個問題?
所有最優秀的
可能類似:http://stackoverflow.com/questions/23667606/detect-when-a-second-annotation-is-selected-in- a-mkmapview – Anna 2014-12-02 11:41:24
接受的答案使用[self performSelector:@selector(checkShouldHideBottomView :) withObject:view afterDelay:0.5]; 我認爲這不適合做。 – jonypz 2014-12-02 19:58:36