我的应用程序中有一个 MapView。显示用户位置处于活动状态。我为长按添加了 GestureRecognizer。一切正常。
不利的是,当我触摸用户的注释(蓝色圆圈)时,未收到手势事件。
怎么才能截获用户注解上的手势呢?
感谢您的帮助。
您可以通过对上述代码稍作修改来添加手势以检测长按。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
UILongPressGestureRecognizer* _longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)];
_longPressRecognizer.minimumPressDuration = 0.0;
_longPressRecognizer.accessibilityValue = [NSString stringWithFormat:@"%d",totalMenues];
[pinView addGestureRecognizer:_longPressRecognizer];
}
- (void) longPressDetected:(UIGestureRecognizer*) gestureRecognizer
{
}
您不是在注释上添加手势。因此您需要为每个注释添加手势。并且由于有两种方法,因此无需向注释添加手势。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
如果你想那么
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation)annotation
{
// Reuse or create annotation view
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapRecgonized:)];
longTap.delegate = self;
[annotationView addGestureRecognizer:longTap];
}
- (void)longTapRecognized:(UITapGestureRecognizer *)recognizer
{
// Handle double tap on annotation view
}
您是否实施了以下方法UIGestureRecognizerDelegate
?
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
该方法应该返回 YES,因此手势识别器可以与其他人一起使用。