【发布时间】:2016-03-01 08:46:29
【问题描述】:
背景 - 我有一个带有标注的地图(MapKit)(添加到注释视图的自定义视图) - 我已经设法确保在标注视图上做出的“长按”手势不会被拾取(见下文,使用“shouldReceiveTouch”)
问题 我不能做的是做同样的事情(即忽略)双击标注视图,因为 MapKit 地图仍然以某种方式拾取并放大。我尝试将双击手势放入陷阱但是它似乎不起作用。
Mapkit 查看代理代码
手势识别器的设置
// Gesture Recogniser (Long Press)
let longPressGR = UILongPressGestureRecognizer(target: self, action: "longPressAction:")
longPressGR.minimumPressDuration = 1
longPressGR.delegate = self
self.mapView.addGestureRecognizer(longPressGR)
// Gesture Recogniser (Double Tap)
let doubleTapGR = UITapGestureRecognizer(target: self, action: "doubleTapAction:")
doubleTapGR.numberOfTapsRequired = 2
doubleTapGR.delegate = self
self.mapView.addGestureRecognizer(doubleTapGR)
为 UIGestureRecognizerDelegate 实现“shouldReceiveTouch”
extension GCMapViewHelper : UIGestureRecognizerDelegate {
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
guard let touchesView = touch.view else { fatalError("ERROR: Touch event received was nil") }
for annotation in self.mapView.selectedAnnotations {
if annotation is GCAnnotation {
let annotationView = self.mapView.viewForAnnotation(annotation)
if let annotationView = annotationView {
if (touchesView.isDescendantOfView(annotationView)) {
return false
}
}
}
}
return true
}
}
编辑:似乎问题是双击手势识别器没有拾取双击...有什么想法吗?即这不起作用..
doubleTapGR.numberOfTapsRequired = 2
EDIT 2这是问题重新代码示例的简化视图:
根据我在打印语句的控制台输出中看到的内容,使用下面的代码: *“longPressAction” - 这出现/有效 *“tapAction” - 这不会出现,即使我双击 MKMapView。所以这是我的问题。
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapview: MKMapView!
func longPressAction() {
print("longPressAction")
}
func tapAction() {
print("tapAction") // ** Not appearing in console **
}
override func viewDidLoad() {
super.viewDidLoad()
// Gesture Recogniser (Long Press)
let longPressGR = UILongPressGestureRecognizer(target: self, action: "longPressAction")
longPressGR.minimumPressDuration = 1
self.mapview.addGestureRecognizer(longPressGR)
let tapGR = UITapGestureRecognizer(target: self, action: "tapAction")
tapGR.numberOfTapsRequired = 2
self.mapview.addGestureRecognizer(tapGR)
//tapGR.requireGestureRecognizerToFail(longPressGR)
}
}
【问题讨论】:
标签: ios swift mapkit uigesturerecognizer mkannotationview