无法识别GMSMapView中子视图上的手势

问题描述 投票:1回答:1

我目前有一个带有UIView子视图的GMSMapView,但是我无法让子视图识别出轻击手势。我已经尝试了许多解决方案,例如将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函数打印出“返回true”。所以我不太确定委托函数如何返回true,但是选择器函数没有触发。我还尝试了另一个UIView的滑动手势,但也没有用。任何帮助表示赞赏,谢谢!

ios swift uigesturerecognizer uitapgesturerecognizer gmsmapview
1个回答
2
投票

输入以下代码:

mapView.settings.consumesGesturesInView = false

来自Google Maps iOS SDK Reference

控制在启用手势时GMSMapView是否完全使用用户的手势(默认为YES)。这可以防止父视图接收这些手势。

当GMSMapView包含在UIScrollView(或其他可滚动区域)中时,这意味着地图上的手势不会作为滚动手势被额外消耗。但是,禁用此选项(设置为NO)可能有助于支持复杂的视图层次结构或要求。

© www.soinside.com 2019 - 2024. All rights reserved.