ios – 单击MKMapView而不会破坏双击缩放

前端之家收集整理的这篇文章主要介绍了ios – 单击MKMapView而不会破坏双击缩放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
用户单击地图时,我想在我的MKMapView上放一个图钉.我有pin码工作,我有单击工作,但当我双击缩放我先点击一下.这是我的代码来设置识别器:
self.doubleTap = [[UITapGestureRecognizer alloc]
                      initWithTarget:self action:@selector(handleDoubleTap:)];
    self.doubleTap.numberOfTapsrequired = 2;
    self.doubleTap.numberOfTouchesrequired = 1;
    [mapView_ addGestureRecognizer:doubleTap_];

    self.singleTap = [[UITapGestureRecognizer alloc]
                      initWithTarget:self action:@selector(handleGesture:)];
    self.singleTap.numberOfTapsrequired = 1;
    self.singleTap.numberOfTouchesrequired = 1;
    [self.singleTap requireGestureRecognizerToFail: doubleTap_];
    [mapView_ addGestureRecognizer:singleTap_];

现在,这引起苹果公司的注意并不奇怪:

Note: In the case of the single-tap versus double-tap gestures,if a
single-tap gesture recognizer doesn’t require the double-tap
recognizer to fail,you should expect to receive your single-tap
actions before your double-tap actions,even in the case of a double
tap. This is expected and desirable behavior because the best user
experience generally involves stackable actions.

所以我将requireGestureRecognizerToFail添加到我的单击识别器中.

[singleTap requireGestureRecognizerToFail: doubleTap];

这确保我的单击识别器不会获得双击.

但…

现在我的双击识别器获得了双击,MKMapView无法获得它们.我已经尝试在识别器中将cancelsTouchesInView设置为NO,但这也没有帮助.

所以我需要一种方法来阻止我的单击识别器获得双击(这似乎不太可能)或者让我的双击事件到我的mapView.

解决方法

发布的代码似乎在iOS 6中正常工作.
双击由地图视图自动处理,不会干扰单击.

在iOS 5.x中,要使地图视图执行其正常的双击处理而不干扰您的单击,请实现shouldRecognizeSimultaneouslyWithGestureRecognizer委托方法并从中返回YES(需要设置双击手势的委托属性)仅识别器).

不确定为什么在iOS 6中没有必要这样做.

猜你在找的iOS相关文章