创建一个UITapGestureRecognizer
并设置numberOfTapsRequired
为2
. 将此手势识别器添加到您的MKPinAnnotationView
. 此外,您需要将您的控制器设置为手势识别器的委托,并实现-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
并返回YES
以防止您的手势识别器踩到MKMapView
.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation)annotation
{
// Reuse or create annotation view
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapRecgonized:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[annotationView addGestureRecognizer:doubleTap];
}
- (void)doubleTapRecognized:(UITapGestureRecognizer *)recognizer
{
// Handle double tap on annotation view
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGesture
{
return YES;
}
编辑:对不起,我误解了。您所描述的内容应该可以使用-mapView:didSelectAnnotationView:
,并且手势识别器配置为仅需要 1 次点击。我们的想法是,我们只会在选择注释视图时将手势识别器添加到注释视图中。当注释视图被取消选择时,我们将删除它。通过这种方式,您可以处理-tapGestureRecognized:
方法中的缩放,并且保证仅在已点击注释时才执行。
为此,我会将手势识别器添加为您的类的属性并在-viewDidLoad
. 假设它被声明为@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
并且我们正在使用 ARC。
- (void)viewDidLoad
{
[super viewDidLoad];
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognized:)];
}
- (void)tapGestureRecognized:(UIGestureRecognizer *)gesture
{
// Zoom in even further on already selected annotation
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView
{
[annotationView addGestureRecognizer:self.tapGesture];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)annotationView
{
[annotationView removeGestureRecgonizer:self.tapGesture];
}