swift - 将谷歌地图的 didTapAtCoordinate 方法与 UITapGestureRecognizer 结合使用

标签 swift uigesturerecognizer

我注意到:

func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
    print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
}

正在覆盖手势识别器:

@IBOutlet weak var mapView: GMSMapView!
let aSelector : Selector = "tappedOnMap"
let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
mapView.userInteractionEnabled = true
tapGesture.numberOfTapsRequired = 1
mapView.addGestureRecognizer(tapGesture)

func tappedOnMap() {
    print("Tapped")
}

“Tapped”从不打印,但“You tapped at so and so”会打印。

我的 map View 被拉伸(stretch)以填满 View 屏幕。似乎 didTapAtCoordinate 覆盖了 GestureRecognizer。 (我确信这一点,因为当我将 gesturerecognizer 附加到该 Controller 的 super View (最顶层 View ),并在 mapView 上禁用“用户交互”时,“Tapped”被打印出来。)如何调用这两种方法?

最佳答案

这是一个 worker 类(Class)来比较你做错了什么:

可能是:

  1. 没有调用 GMSMapViewDelegate

  2. MapView.delegate = self 未设置

  3. 缺少 func mapView(**_** MapView: GMSMapView, didTap marker: GMSMarker) -> Bool

    前面的下划线
    class CustomMapPicker: UIViewController, GMSMapViewDelegate
    {
        @IBOutlet weak var MapView: GMSMapView!
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        MapView.delegate = self
        let camera = GMSCameraPosition.camera(withLatitude: 25.2048, longitude: 55.2708, zoom: 10)
        MapView?.camera = camera
        MapView?.animate(to: camera)
        MapView?.isMyLocationEnabled = false
    }
    
    func mapView(_ MapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
    {
        print("lat = " + "\(coordinate.latitude)" + " long = " +  "\(coordinate.longitude)")
    }
    
    func mapView(_ MapView: GMSMapView, didTap marker: GMSMarker) -> Bool
    {
        return true
    }
    }
    

关于swift - 将谷歌地图的 didTapAtCoordinate 方法与 UITapGestureRecognizer 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34535923/

相关文章:

ios - UITapGestureRecognizer sender 是手势,不是ui对象

ios - 区分 UITableViewCell 上的单击和双击

ios - 如何将手势识别器添加到 UILabel?

Swift 3 函数以格式化字符串打印出最近 7 天

ios - 为什么即使包含应用程序在前台,iOS 小部件也不会及时刷新

ios - 我的团队成员如何提交完整的崩溃报告供我在 Xcode 中检查?

ios - UIPanGestureRecognizer 无法识别平移小 Action (<3mm)

ios - UILongPressGestureRecognizer 的工作原理

ios - dispatch_group_wait 不等待

ios - Swift 在 map 上添加注释