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

时间:2019-03-01 02:39:43

标签: ios swift uigesturerecognizer uitapgesturerecognizer gmsmapview

我目前有一个带有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函数中打印出“ returned true”。所以我不太确定委托函数如何返回true,但是选择器函数没有触发。我还尝试了另一个UIView的滑动手势,但也没有用。感谢您的任何帮助,谢谢!!

1 个答案:

答案 0 :(得分:2)

输入以下代码:

mapView.settings.consumesGesturesInView = false

来自Google Maps iOS SDK Reference

  

控制用户的手势是否完全被用户的手势消耗   启用手势时为GMSMapView(默认为YES)。这样可以防止   这些手势来自父视图。

     

当UIScrollView(或其他)包含GMSMapView时   可滚动区域),这意味着地图上的手势不会   其他消耗为滚动手势。但是,禁用此设置   到否)可能对支持复杂的视图层次结构或   要求。