iOS MapKit:通过点击地图关闭注释弹出窗口

6
我有一个MapKit应用程序,可以在地图上放置注释。当您按下它们时,它会显示具有标题属性的标注。这个功能很好,但用户无法关闭它们。它们会一直保持打开状态,直到用户点击另一个注释。我能不能让用户点击地图其他位置(或再次点击注释)来关闭它?我觉得这是默认设置,所以可能是我做错了什么?我有一个手势识别器,用于检测一些地图点击。
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                            initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;

[self.mapView addGestureRecognizer: tap];

这会触发以下操作:

- (void)handleTap:(UITapGestureRecognizer *)sender {     
if (sender.state == UIGestureRecognizerStateEnded)  {   


    CGPoint tapPoint = [sender locationInView:sender.view.superview];
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint: tapPoint toCoordinateFromView: self.mapView];

    if (pitStopMode && !pitStopMade){
            pitStopMade = YES;
        InfoAnnotation *annotation = [[InfoAnnotation alloc]
                       initNewPitstopWithCoordinate:coordinate];
        NSLog(@" Created Pit Stop");

        annotation.draggable = NO;
        //place it on the map
        [self.mapView addAnnotation: annotation];

        self.instructionLabel.text = @"Tap button again to remove";
        annotation.creatorId = self.localUser.deviceId;
        //send it to the server
        [annotation updateLocationWithServerForConvoy: self.convoyCode];        

        [annotation release];

    }  

    if (hazardMode && !hazardMade){
            hazardMade = YES;
        InfoAnnotation *annotation  = [[InfoAnnotation alloc]
                       initNewHazardWithCoordinate:coordinate];         
        NSLog(@" Created Hazard");

        annotation.draggable = NO;
        //place it on the map
        [self.mapView addAnnotation: annotation];

        self.instructionLabel.text = @"Tap button again to remove";
        annotation.creatorId = self.localUser.deviceId;
        //send it to the server
        [annotation updateLocationWithServerForConvoy: self.convoyCode];        

        [annotation release];


    }
}

我需要让这些轻拍事件传递到地图视图,是否还需要进行其他操作?尽管注释的拖动和轻拍功能正常,但我不确定是否会影响此问题。

我是否遗漏了某个选项,还是必须手动实现这个功能?

2个回答

10
你可以实现 UIGestureRecognizerDelegate 方法 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 并返回 YES(这样地图视图自己的轻拍手势识别器也会执行其方法)。
首先,在你的视图控制器接口中添加协议声明(以避免编译器警告):
@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>

接下来,在手势识别器上设置delegate属性:
tap.delegate = self;

最后,实现该方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer:
        (UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

如果由于某些原因无法实现,您可以在handleTap:方法的顶部手动取消选择任何当前选定的注释。
for (id<MKAnnotation> ann in mapView.selectedAnnotations) {
    [mapView deselectAnnotation:ann animated:NO];
}

尽管地图视图一次只允许选择一个标注,但selectedAnnotations属性是一个NSArray,因此我们需要循环遍历它。

0

Anna 讲解得很好。我也想用准确的代码来解释。你可以这样做

     UITapGestureRecognizer *tapMap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeCallout:)];
      [self.mapView addGestureRecognizer:tapMap];

-(void) closeCallout:(UIGestureRecognizer*) recognizer 
        {
           for (id<MKAnnotation> ann in mapView.selectedAnnotations) 
           {
               [mapView deselectAnnotation:ann animated:NO];
           }      


        }   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接