Double selection touch on MKPinAnnotationView



编辑:将标题从:"双击.."更改为"双击触摸.."

我需要在我的应用程序中检测到至少第二次触摸MKPinAnnotationView。目前我可以进行第一次触摸(我在这里使用kvo:在MKMapView中选择MKAnnotation时进行检测),第一次触摸效果很好),但如果我再次点击引脚,则不会调用任何内容,因为所选值不会更改。我使用"mapView:didSelectAnnotationView:"尝试了同样的操作,该操作自ios 4以来一直有效,但在第二次点击时也不会再次调用。

如果有人能帮我,那就太好了!

致以最诚挚的问候

编辑,添加更多信息:

因此,触摸不必很快,如果用户触摸了引脚,将在注释的标题和副标题中显示消息,如果用户再次触摸同一个引脚,那么我将对做另一件事

创建一个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];
}

最新更新