2014-12-02 72 views
2

我有我在哪里执行以下操作注释的地图:检查是否用户已经选择了另一个注释在地图

- (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"); 
     } 

    } 

}} 

然而,这并没有捕捉到所有的接触,再加上它是一个有点意大利面条解决方案。

任何想法如何解决这个问题?

所有最优秀的

+0

可能类似:http://stackoverflow.com/questions/23667606/detect-when-a-second-annotation-is-selected-in- a-mkmapview – Anna 2014-12-02 11:41:24

+0

接受的答案使用[self performSelector:@selector(checkShouldHideBottomView :) withObject:view afterDelay:0.5]; 我认为这不适合做。 – jonypz 2014-12-02 19:58:36

回答

0

委托不这样做,你不选择,然后选择另一注释视图和的情况下,你选择一个新的注解视图,而已经选择了一个外壳之间的任何区别。在这两种情况下,这些调用的顺序进行:

  • didDeselect厂景
  • didSelect视图2

你想两件事情:

1)不叫的methodB当视图2的选择非常接近(及时)取消选择观点1。

2)当选择关闭(在时间上)到先前的选择时,不调用方法A.

要解决这两个问题,可以存储两个时间戳:1)上次选择2)上次取消选择。有了这两个变量,您可以设置一个阈值来定义上面两条规则中“close”的含义。

现在,您有一个条件可以防止在快速取消选择和选择的特定情况下(这是您从一个选择切换到另一个时委托实际发生的情况)调用methodA和methodB。

+0

我相信时间戳不会解决我的问题。 用户应该能够从注释点击注释到没有任何外部行为(添加或删除其他视图 - > methodA和methodB) – jonypz 2014-12-02 08:40:04

+0

我更新了我的答案。现在应该清楚了。 – ffarquet 2014-12-02 13:45:45

+0

只有当我将阈值设置为无穷大时,这才会起作用,并且当用户在注释外单击时应重置此值。这仍然是一个粗略的解决方案。 – jonypz 2014-12-02 20:03:27

0
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView 
{ 
    indexPathTag=aView.tag; 
    [mapView deselectAnnotation:aView.annotation animated:YES]; 

} 
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)aView 
{ 
} 

从didSelectAnnotationView调用deselectAnnotation。

+0

不起作用。同样的行为 – jonypz 2014-12-02 09:23:43

0

这是我目前的解决方案。尽管我不喜欢它,但现在它仍然有效。 如果你有更好的,请发表一个答案,我会将其标记为正确的。

我正在使用全局变量来跟踪当前选定的注释。我将它初始化为零。

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{ 

[self performSelector:@selector(numberOfSelectedAnnotations) 
      withObject:view.annotation afterDelay:.5]; 
} 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
     if (self.selectedAnnotation == nil) { 
     [self methodB]; 
     } 
    self.selectedAnnotation = (MKAnnotationView*)view; 
} 


-(void)numberOfSelectedAnnotations{ 
if (self.mapView.selectedAnnotations.count == 0) { 
    [self methodA]; 
    self.selectedAnnotation = nil; 
    } 
} 
-1

这是我的优选解决方案:

private var selectedAnnotationView: MKAnnotationView? 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if let _ = annotation as? MKUserLocation { 
     return nil 
    } 
    let ident = "MyAnnotation" 
    let pin = mapView.dequeueReusableAnnotationViewWithIdentifier(ident) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: ident) 
    pin.animatesDrop = true 
    pin.canShowCallout = true 
    pin.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "annotationTapped:")) 
    pin.rightCalloutAccessoryView = UIButton(type: .InfoDark) 
    return pin 
} 

// This will get called before didDeselectAnnotationView below 
func annotationTapped(tapGesture: UITapGestureRecognizer) { 
    if let annotationView = tapGesture.view as? MKAnnotationView { 

     if annotationView === self.selectedAnnotationView { 
      // same as last time 
     } else { 
      // different annotation view 
     } 

     self.selectedAnnotationView = annotationView 

     // any other logic for handling annotation view tap 
    } 
} 

func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) { 
    if view === self.selectedAnnotationView { 

     // The user did not select another annotation view 
     // (e.g. they tapped the map)    

     self.selectedAnnotationView = nil 
    } 
} 
相关问题