无法识别 GMSMapView 中子视图上的手势
Unable to recognize gestures on a subview in a GMSMapView
我目前有一个带有 UIView 子视图的 GMSMapView,但我无法让子视图识别点击手势。我已经尝试了很多解决方案,例如将 isUserInteractionEnabled
设置为 true 并覆盖 delegate
但 none 到目前为止已经奏效了。
import UIKit
import GoogleMaps
class MapViewController: UIViewController, UIGestureRecognizerDelegate {
var testView: UIView!
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 15.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isUserInteractionEnabled = true
self.view = mapView
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
testView = UIView()
testView.backgroundColor = UIColor(white: 0, alpha: 0.5)
testView.frame.origin = CGPoint(x: 0, y: 0);
testView.frame.size = CGSize(width: screenWidth, height: screenHeight)
testView.isUserInteractionEnabled = true
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.doSomething(_:)))
gesture.numberOfTapsRequired = 1
gesture.numberOfTouchesRequired = 1
gesture.delegate = self
self.view.addSubview(testView)
testView.addGestureRecognizer(gesture)
}
@objc func doSomething(_ sender: UIGestureRecognizer) {
print("doSomething")
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if (touch.view == gestureRecognizer.view) {
print("returned true")
return true
}
return false
}
}
有趣的是,当我点击 testView
时,它会从我的 shouldReceiveTouch
函数中打印出 "returned true"。所以我不太确定委托函数 returns 如何为真,但选择器函数没有触发。我还用另一个 UIView 尝试了这种滑动手势,但也没有用。感谢您的帮助,提前致谢!
输入以下代码:
mapView.settings.consumesGesturesInView = false
来自Google Maps iOS SDK Reference:
Controls whether gestures by users are completely consumed by the
GMSMapView when gestures are enabled (default YES). This prevents
these gestures from being received by parent views.
When the GMSMapView is contained by a UIScrollView (or other
scrollable area), this means that gestures on the map will not be
additional consumed as scroll gestures. However, disabling this (set
to NO) may be useful to support complex view hierarchies or
requirements.
我目前有一个带有 UIView 子视图的 GMSMapView,但我无法让子视图识别点击手势。我已经尝试了很多解决方案,例如将 isUserInteractionEnabled
设置为 true 并覆盖 delegate
但 none 到目前为止已经奏效了。
import UIKit
import GoogleMaps
class MapViewController: UIViewController, UIGestureRecognizerDelegate {
var testView: UIView!
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 15.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isUserInteractionEnabled = true
self.view = mapView
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
testView = UIView()
testView.backgroundColor = UIColor(white: 0, alpha: 0.5)
testView.frame.origin = CGPoint(x: 0, y: 0);
testView.frame.size = CGSize(width: screenWidth, height: screenHeight)
testView.isUserInteractionEnabled = true
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.doSomething(_:)))
gesture.numberOfTapsRequired = 1
gesture.numberOfTouchesRequired = 1
gesture.delegate = self
self.view.addSubview(testView)
testView.addGestureRecognizer(gesture)
}
@objc func doSomething(_ sender: UIGestureRecognizer) {
print("doSomething")
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if (touch.view == gestureRecognizer.view) {
print("returned true")
return true
}
return false
}
}
有趣的是,当我点击 testView
时,它会从我的 shouldReceiveTouch
函数中打印出 "returned true"。所以我不太确定委托函数 returns 如何为真,但选择器函数没有触发。我还用另一个 UIView 尝试了这种滑动手势,但也没有用。感谢您的帮助,提前致谢!
输入以下代码:
mapView.settings.consumesGesturesInView = false
来自Google Maps iOS SDK Reference:
Controls whether gestures by users are completely consumed by the GMSMapView when gestures are enabled (default YES). This prevents these gestures from being received by parent views.
When the GMSMapView is contained by a UIScrollView (or other scrollable area), this means that gestures on the map will not be additional consumed as scroll gestures. However, disabling this (set to NO) may be useful to support complex view hierarchies or requirements.