我正在尝试向UIPanGestureRecognizer
添加mapView
,但我不知道为什么动作方法永远不会被调用(Swift 2.1)。
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIGestureRecognizerDelegate
{
override func viewDidLoad()
{
super.viewDidLoad()
// other stuff...
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:")
gestureRecognizer.delegate = self
self.mapView.addGestureRecognizer(gestureRecognizer)
}
func didDragMap(sender: UIPanGestureRecognizer)
{
// never enter here
}
}
这里有什么问题?我有相同的Objective-C对应,它的工作。
答案 0 :(得分:3)
由于MKMapView已经处理了它自己的手势,你需要让它也能听你的手势。
实施shouldRecognizeSimultaneouslyWithGestureRecognizer
并返回true,如下所示:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}