MKPinAnnotationView 上的双选触摸

Yot*_*tes 0 iphone mkpinannotationview mkannotation mkannotationview ios

编辑:将标题从“双击..”更改为“双击选择触摸..”

我需要在我的应用程序中检测至少对 MKPinAnnotationView 的第二次触摸。目前我能够获得第一次触摸(我从这里使用 kvo:Detecting when MKAnnotation is selected in MKMapView),并且它在第一次触摸时效果很好),但是如果我再次点击引脚,则不会调用任何内容,因为所选值不会改变。我尝试使用自 ios 4 以来有效的“mapView:didSelectAnnotationView:”进行相同的操作,但在第二次点击时也不会再次调用它。

如果有人能帮助我解决这个问题,那就太好了!

此致

编辑,添加更多信息:

因此,触摸不必很快,如果用户触摸图钉,我会在注释的标题和副标题中显示一条消息,如果用户再次触摸同一个图钉,所以我会用它做另一件事

Mar*_*ams 5

创建一个UITapGestureRecognizer并设置numberOfTapsRequired2. 将此手势识别器添加到您的 实例中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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:抱歉我误解了。您所描述的应该可以使用-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];
}
Run Code Online (Sandbox Code Playgroud)