我正在尝试将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对应项,并且可以正常工作。

最佳答案

由于MKMapView已经处理了它自己的手势,因此需要使它也可以侦听您的手势。

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

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

Source

关于ios - UIPanGestureRecognizer永不调用操作方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36165235/

10-10 11:43