UIPanGestureRecognizer从不调用action方法

时间:2016-03-22 21:07:18

标签: ios swift mapkit action uipangesturerecognizer

我正在尝试向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对应,它的工作。

1 个答案:

答案 0 :(得分:3)

由于MKMapView已经处理了它自己的手势,你需要让它也能听你的手势。

实施shouldRecognizeSimultaneouslyWithGestureRecognizer并返回true,如下所示:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

Source