编辑:将标题从:“Double tap ..”更改为“Double selection touch..”
我需要在我的应用程序中至少检测到对 MKPinAnnotationView 的第二次触摸。目前我可以第一次触摸(我在这里使用 kvo:Detecting when MKAnnotation is selected in MKMapView),第一次触摸效果很好),但是如果我再次点击 pin,什么都不会被调用,因为选择值不变。我使用自 ios 4 起有效的“mapView:didSelectAnnotationView:”进行了相同的尝试,但在第二次点击时也不会再次调用它。
如果有人能帮我解决这个问题,那就太好了!
最好的问候
编辑,添加更多信息:
因此,触摸不必很快,如果用户触摸了图钉,我会在注释的标题和副标题中显示一条消息,如果用户再次触摸同一个图钉,那么我会再做一次那个东西
最佳答案
创建一个 UITapGestureRecognizer
并将 numberOfTapsRequired
设置为 2
。将此手势识别器添加到您的 MKPinAnnotationView
实例。此外,您需要将 Controller 设置为手势识别器的委托(delegate)并实现 -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 次点击的手势识别器。我们的想法是,我们只会在选中时将手势识别器添加到注释 View 中。当取消选择注释 View 时,我们将删除它。通过这种方式,您可以在 -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];
}
关于iphone - MKPinAnnotationView 上的双选触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805823/