0

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

我需要在我的应用程序中检测到至少第二次触摸 MKPinAnnotationView。目前我能够获得第一次触摸(我从这里使用 kvo:Detecting when MKAnnotation is selected in MKMapView),并且在第一次触摸时效果很好),但是如果我再次点击 pin,什么都不会调用,因为选定的值不会改变。我使用“mapView:didSelectAnnotationView:”尝试了相同的操作,该方法从 ios 4 开始有效,但在第二次点击时也不会再次调用它。

如果有人可以帮助我,那就太好了!

最好的祝福

编辑,添加更多信息:

因此,触摸不一定要很快,如果用户触摸 pin,将在注释的标题和副标题中显示一条消息,如果用户再次触摸同一个 pin,那么我会用它做另一件事

4

1 回答 1

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;
}

编辑:对不起,我误解了。您所描述的内容应该可以使用-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];
}
于 2012-01-10T17:01:06.250 回答