【问题标题】:mapView:didDeselectAnnotationView: delegate method getting called before the annotation view is actually deselectedmapView:didDeselectAnnotationView: 在实际取消选择注释视图之前调用委托方法
【发布时间】:2011-10-19 09:49:51
【问题描述】:

我正在使用填充了自定义图钉的地图视图。当用户点击地图上的某处以取消选择引脚时,我想实现地图以使引脚不会被取消选择(即用户无法在不选择其他引脚的情况下取消选择引脚,因此始终会选择至少一个引脚)。这是我对 didDeselectAnnotationView 方法的实现:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [mapView selectAnnotation:view.annotation animated:NO];
}

基本上,我正在尝试重新选择注释。但是,经过一些调试并打印到控制台后,我意识到注释视图实际上并没有被取消选择,直到方法 didDeselectAnnotationView: 完成运行(也就是说,事件的顺序是:用户点击地图上的某个地方,didDeselectAnnotationView: 被调用并完成执行,注释视图实际上被取消选择)。有没有其他人遇到过这个问题,或者有没有人知道另一种方法来强制地图的行为,这样用户在不选择其他引脚的情况下无法取消选择引脚,从而始终选择一个引脚?

感谢您的帮助。

【问题讨论】:

    标签: ios annotations mkmapview mkannotationview mkmapviewdelegate


    【解决方案1】:

    尝试将重新选择推迟到didDeselectAnnotationView 完成后:

    -(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
    {
        [self performSelector:@selector(reSelectAnnotationIfNoneSelected:) 
                withObject:view.annotation afterDelay:0];
    }
    
    - (void)reSelectAnnotationIfNoneSelected:(id<MKAnnotation>)annotation
    {
        if (mapView.selectedAnnotations.count == 0)
            [mapView selectAnnotation:annotation animated:NO];
    }
    

    【讨论】:

    • 对我来说,这仅适用于annotationView.canShowCallout = false
    【解决方案2】:

    编辑:请记住,此方法可防止选择任何其他引脚,这在创建此答案时是未知的。很有可能,这不是您想要的行为。

    我知道这是一个老问题,但接受的答案在 iOS 8 中对我不起作用。对我有用的是完全禁用 UITapGestureRecognizer,它默认包含在 MKMapView 中。

    - (void)disableTapRecognizerForMapView:(MKMapView *)mapView {
        NSArray *a = [[self.mapView.subviews objectAtIndex:0] gestureRecognizers];
    
        for (id gesture in a)
            if ([gesture isKindOfClass:[UITapGestureRecognizer class]])
                [gesture setEnabled:NO];
    }
    

    希望这对其他人有所帮助。

    干杯!

    【讨论】:

    • 如果我这样做,就不可能在地图上选择任何注解。你是怎么解决这个问题的?
    • @gebirgsbärbel 老实说,我不记得我是如何解决这个问题的,但我的应用程序中的当前行为是允许用户没有选择任何引脚——这让我觉得我没有找到解决方法。我将更新答案以反映这种行为。
    • 非常感谢您的快速回答
    【解决方案3】:

    我遇到了类似的问题,但反过来。

    根据选择的引脚,表格将滚动到相应的单元格。如果未选择任何引脚,则表格将滚动回第一个单元格。正在调用取消选择方法的同时在选择另一个引脚时调用SELECT方法并且表格不会根据需要滚动。

    以下代码解决了这个问题,是对 Anna 的解决方案的轻微修改。

    - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view
    {
       if ([view.annotation isKindOfClass:[MKUserLocation class]]) 
            return;
    
        [self performSelector:@selector(resetTableScroll:) 
               withObject:view.annotation afterDelay:.5];
        }
    
    
    
    - (void)resetTableScroll:(id<MKAnnotation>)annotation{
    
        if (theMap.selectedAnnotations.count == 0)
        {
            NSIndexPath *position = [NSIndexPath indexPathForRow:0 inSection:0];
            [[self theTable] scrollToRowAtIndexPath:position atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
        }}
    

    【讨论】:

      【解决方案4】:

      安娜的回答很棒。 这是 Swift 3 或 4 中的答案:D

      func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
          perform(#selector(MyViewController.reSelectAnnotationIfNoneSelected(_:)), with: view.annotation, afterDelay: 0)
      }
      
      func reSelectAnnotationIfNoneSelected(_ annotation: MKAnnotation) {
          if mapView.selectedAnnotations.count == 0 {
              mapView.selectAnnotation(annotation, animated: false)
          }
      }
      

      【讨论】:

        【解决方案5】:

        如果您为地图视图添加手势,请尝试以下操作:

        - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
        

        返回 NO,它有效

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-02
          相关资源
          最近更新 更多