【问题标题】:Why is double tap still being picked up in this IOS code?为什么在这个 IOS 代码中仍然会出现双击?
【发布时间】: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


    【解决方案1】:

    虽然这不是您问题的直接答案 - 我有一个解决方法。

    如果您将手势识别器添加到自定义注释视图中,那么您可以在注释视图和任何关联的标注上捕获长按和双击。

    我写了一些示例代码来试试这个:

    import UIKit
    import MapKit
    
    class StarAnnotation : NSObject, MKAnnotation {
        let title: String?
        let coordinate: CLLocationCoordinate2D
    
        init(title: String, coordinate: CLLocationCoordinate2D) {
            self.title = title
            self.coordinate = coordinate
            super.init()
        }
    }
    
    class StarAnnotationView : MKAnnotationView, UIGestureRecognizerDelegate {
        override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
            super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
            self.image = UIImage(named: "star")
    
            // Gesture Recogniser (Long Press)
            let longPressGR = UILongPressGestureRecognizer(target: self, action: "longPressAction:")
            longPressGR.minimumPressDuration = 1
            longPressGR.delegate = self
            self.addGestureRecognizer(longPressGR)
    
            // Gesture Recogniser (Double Tap)
            let doubleTapGR = UITapGestureRecognizer(target: self, action: "doubleTapAction:")
            doubleTapGR.numberOfTapsRequired = 2
            doubleTapGR.delegate = self
            self.addGestureRecognizer(doubleTapGR)
    
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        func doubleTapAction(recognizer: UITapGestureRecognizer) {
            if recognizer.numberOfTapsRequired == 2 {
                print("double tapped")
            }
        }
    
        func longPressAction(recognizer: UILongPressGestureRecognizer) {
            print("long pressed")
        }
    }
    
    class ViewController: UIViewController, MKMapViewDelegate {
    
        @IBOutlet weak var mapView: MKMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // set up map view and sample annotation
            let operaHouseCoord = CLLocationCoordinate2D(latitude: -33.8587, longitude: 151.2148)
            let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
            let operaHouseAnnotation = StarAnnotation(title: "Opera House", coordinate: operaHouseCoord)
            mapView.region = MKCoordinateRegion(center: operaHouseCoord, span: span)
            mapView.delegate = self
            mapView.addAnnotation(operaHouseAnnotation)
        }
    
        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
            if let annotation = annotation as? StarAnnotation {
                let identifier = "star"
                if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? StarAnnotationView {
                    dequeuedView.annotation = annotation
                    return dequeuedView
                } else {
                    let view = StarAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                    view.canShowCallout = true
                    return view
                }
            }
            return nil
        }
    }
    

    【讨论】:

    • 谢谢,但在我的情况下,长按和双击是来自地图视图本身的。只是我不希望它们在触摸/按下点位于标注视图上方时发生。虽然我已经困住了长按,但双击似乎难以捉摸......就像 mapkit 让它发生,不管它应该让我访问它的事实如何
    • 好的..对不起..也许我误解了你想要达到的目标。我以为您在双击标注时试图阻止在地图视图上注册双击(即放大)?通过将双击手势识别器分配给标注 - 它可以防止它掉到地图上并发生放大。
    • 哦,。所以我可能误解了你。让我试一试并报告。感谢您的回复。
    • 很有魅力!谢谢。很抱歉最初没有理解您的建议。我想知道它是否适用于标注视图以及注释视图本身。它确实有效,毫无疑问,因为标注视图是我猜的 annotationView 的子视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多