gpt4 book ai didi

ios - 无法识别 GMSMapView 中 subview 上的手势

转载 作者:搜寻专家 更新时间:2023-11-01 06:54:06 25 4
gpt4 key购买 nike

我目前有一个带有 UIView subview 的 GMSMapView,但我无法让 subview 识别点击手势。我已经尝试了很多解决方案,例如将 isUserInteractionEnabled 设置为 true 并覆盖 delegate,但到目前为止都没有奏效。

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”。所以我不太确定委托(delegate)函数如何返回 true,但选择器函数没有触发。我还用另一个 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.

关于ios - 无法识别 GMSMapView 中 subview 上的手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54937202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com