长按Google地图
帮我解决问题。 我试图在Google地图上长时间跟踪地图,但是我无法做到这一点。 这是我的代码的一个例子:
import UIKit import GoogleMaps class ViewController: UIViewController { @IBOutlet var mMap: GMSMapView! var longPressRecognizer = UILongPressGestureRecognizer() @IBAction func longPress(_ sender: UILongPressGestureRecognizer) { testTextview.text = "You tapped at YES" } override func viewDidLoad() { super.viewDidLoad() longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress)) longPressRecognizer.minimumPressDuration = 0.5 mMap.addGestureRecognizer(longPressRecognizer) mMap.isMyLocationEnabled = true mMap.settings.compassButton = true mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200, longitude: 52.3154000, zoom: 15.0) } }
使用这个代码不会发生。 我尝试了所有在stackoverflow上的方法,但也没有发生任何事情。
GMSMapView
类有很多不同的手势识别器,所以你需要添加一个UIGestureRecognizerDelegate
实现,把这个方法添加到你的viewController shouldRecognizeSimultaneouslyWith
识别shouldRecognizeSimultaneouslyWith
这里是你需要修改的
加
extension ViewController : UIGestureRecognizerDelegate { public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } }
加
longPressRecognizer.delegate = self
在这行下面 longPressRecognizer.minimumPressDuration = 0.5
完整的代码
import UIKit import GoogleMaps class ViewController: UIViewController { @IBOutlet var mMap: GMSMapView! var longPressRecognizer = UILongPressGestureRecognizer() @IBAction func longPress(_ sender: UILongPressGestureRecognizer) { testTextview.text = "You tapped at YES" } override func viewDidLoad() { super.viewDidLoad() longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress)) longPressRecognizer.minimumPressDuration = 0.5 longPressRecognizer.delegate = self mMap.addGestureRecognizer(longPressRecognizer) mMap.isMyLocationEnabled = true mMap.settings.compassButton = true mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200, longitude: 52.3154000, zoom: 15.0) } } extension ViewController : UIGestureRecognizerDelegate { public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } }