ios mapkit通过点击地图closures注释标注
我有一个mapkit应用程序,它将注释放在地图上,当你按下它时,它会显示标题属性的标注。
这工作正常,但用户不能closures它们。 他们保持开放,直到他们点击另一个注释。 我不能让用户可以在地图上点击其他地方(或再次点击注释)closures它吗?
我有一种感觉,这是默认设置,所以也许我正在做的东西是填塞? 我有一个手势识别器,我用它来检测一些地图水龙头
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]; } }
}
我还有什么要让这些水龙头去mapview? 在注释上拖动和敲击工作正常,但我不知道这是什么原因造成的?
有没有我失踪的选项,还是我必须尝试手动实施这个?
您可以实现UIGestureRecognizerDelegate
方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
并返回YES
(以便地图视图自己的轻gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
手势识别器也将执行其方法)。
首先,将协议声明添加到您的视图控制器的接口(以避免编译器警告):
@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
接下来,在手势识别器上设置delegate
属性:
tap.delegate = self;
最后,实施方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *)otherGestureRecognizer { return YES; }
如果由于某种原因handleTap:
,则可以在handleTap:
方法的顶部手动取消select当前选定的任何注释:
for (id<MKAnnotation> ann in mapView.selectedAnnotations) { [mapView deselectAnnotation:ann animated:NO]; }
即使地图视图一次最多只能select一个注释, selectedAnnotations
属性也是一个NSArray
所以我们通过它循环。
安娜解释得很好。我也想用代码来解释。 你可以这样做
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]; } }