为什么在此IOS代码中仍会双击? [英] Why is double tap still being picked up in this IOS code?

查看:69
本文介绍了为什么在此IOS代码中仍会双击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景 -我有一个带有标注的地图(MapKit)(将自定义视图添加到注释视图) -我设法确保在标注视图上做出的长按"手势不会被拾取(请参见下文,使用"shouldReceiveTouch")

Background - I have a map (MapKit) with a callout (custom view added to an annotation view) - I have managed to ensure that the "long press" gesture made over the callout view does not get picked up (see below, using "shouldReceiveTouch")

问题 我不能做的就是在标注视图上双击做同样的事情(即忽略),因为MapKit地图仍然以某种方式将其放大并放大.但是它似乎不起作用.

Issue What I can't do is do the same thing (i.e. ignore) a double-click over the callout view, in that the MapKit map still somehow picks this up and zooms in. I've tried putting the Double Tap gesture in to trap it however it doesn't seem to be working.

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
 }
}

看来问题是双击手势识别器没有拿起双击...任何想法吗?也就是说,这不起作用.

It seems the issue is the double-tap gesture recognizer is NOT picking up the double tap...any ideas? i.e. this is not working..

doubleTapGR.numberOfTapsRequired = 2

编辑2 以下是问题重新编码示例的简化视图:

EDIT 2 Here is a simplified view of the issue re code example:

使用以下代码,根据我在打印语句的控制台输出中看到的内容: *"longPressAction"-出现/有效 *"tapAction"-即使双击MKMapView也不会出现.所以这是我的问题.

With the code below in terms of what I see in the console output from print statements: * "longPressAction" - This appears/works * "tapAction" - This does NOT appear, even when I double tap over the MKMapView. So this is my isssue here.

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)

    }

}

推荐答案

虽然这不是您问题的直接答案-我为您提供了一种解决方法.

While it's not a direct answer to your question - I have a workaround for you.

如果将手势识别器添加到自定义注释视图中,则可以捕获长按并双击注释视图和任何关联的标注.

If you add your gesture recognizers to your custom annotation view then you can capture both long press and double tap over the annotation view and any associated callout.

我为尝试此操作而编写的一些示例代码:

Some example code I wrote to try this:

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
    }
}

这篇关于为什么在此IOS代码中仍会双击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆