带有MKMapKit的Swift GestureRecognizers - Drop pin和drag

 刻骨铭心2502914183_610 发布于 2023-01-02 16:48

我试图用一些UIGestureReconizers带有MKMapView上,用户可以放置图钉并拖动它周围.它有一个callout.我正在实现这个TabbarController,也在一个NavigationController.我目前有:

1)A PanGestureRecognizer将屏幕上的Tabbar和Navigation项目设置为动画.这样可以正常运行而不会干扰平移地图.

2)TapGestureRecognizer设置为一个水龙头将1)中的两个项目动画回屏幕.

3)TapGestureRecognizer设置为两个点击允许底层MKMapView缩放功能工作.这GestureRecognizer's delegate有gestureRecognizer.shouldRecognizeSimultaneouslyWithGestureRecognizer调成true

这些设置viewDidLoad如下:

 // This sets up the pan gesture recognizer to hide the bars from the UI.
    let panRec: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:")
    panRec.delegate = self
    mapView.addGestureRecognizer(panRec)

    // This sets up the tap gesture recognizer to un-hide the bars from the UI.
    let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didTapMap:")
    singleTap.delegate = self
    singleTap.numberOfTapsRequired = 1
    singleTap.numberOfTouchesRequired = 1
    mapView.addGestureRecognizer(singleTap)

    // This sets up the double tap gesture recognizer to enable the zoom facility.
    // In order to pass double-taps to the underlying `MKMapView` the delegate for this recognizer (self) needs to return true from
    // gestureRecognizer.shouldRecognizeSimultaneouslyWithGestureRecognizer
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer()
    doubleTap.numberOfTapsRequired = 2
    doubleTap.numberOfTouchesRequired = 1
    doubleTap.delegate = self
    mapView.addGestureRecognizer(doubleTap)

// This delays the single-tap recognizer slightly and ensures that it will NOT fire if there is a double-tap
    singleTap.requireGestureRecognizerToFail(doubleTap)

当我尝试实现一个UILongPressGestureRecognizer允许将引脚丢弃到地图上时,我的问题就出现了.我正在尝试使用以下内容添加到viewDidLoad:

// This sets up the long tap to drop the pin.
    let longTap: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "didLongTapMap:")
    longTap.delegate = self
    longTap.numberOfTapsRequired = 0
    longTap.minimumPressDuration = 0.5
    mapView.addGestureRecognizer(longTap)

这是我的行动方法:

func didLongTapMap(gestureRecognizer: UIGestureRecognizer) {
    // Get the spot that was tapped.
    let tapPoint: CGPoint = gestureRecognizer.locationInView(mapView)
    let touchMapCoordinate: CLLocationCoordinate2D = mapView.convertPoint(tapPoint, toCoordinateFromView: mapView)

    var viewAtBottomOfHierarchy: UIView = mapView.hitTest(tapPoint, withEvent: nil)
    if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView {
        return
    } else {
        if .Began == gestureRecognizer.state {
            // Delete any existing annotations.
            if mapView.annotations.count != 0 {
                mapView.removeAnnotations(mapView.annotations)
            }

            annotation = MKPointAnnotation()
            annotation.coordinate = touchMapCoordinate

            mapView.addAnnotation(annotation)
            _isPinOnMap = true

            findAddressFromCoordinate(annotation.coordinate)
            updateLabels()
        }
    }
}

这确实允许引脚在长抽头上掉落并且单击将显示标注但是如果拖动没有足够快地启动则第二次敲击以保持并拖动导致第二引脚下降.第二个引脚落入前一个引脚悬停的空间,可以被用户拖动,但新的引脚丢失看起来很笨拙.

我正在尝试使用该行:

if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView {

将tap返回到MKMapView并防止另一个引脚被丢弃但是返回永远不会被调用,即使此行上的断点显示viewAtBottom属于类型MapKit.MKPinAnnotationView.我出错的任何想法?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有