1

I currently have a GMSMapView with a UIView subview, but I can't get the subview to recognize tap gestures. I've tried many solutions like setting isUserInteractionEnabled equal to true and overriding the delegate but none have worked so far.

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
    }
}

The interesting thing is that when I do tap on testView, it does print out "returned true" from my shouldReceiveTouch function. So I'm not quite sure how the delegate function returns true, yet the selector function isn't firing. I also tried this swipe gestures with another UIView and that did not work either. Any help is appreciated, thank you in advance!

Chris Gong
  • 8,031
  • 4
  • 30
  • 51
  • Isn't one supposed to tap the map to zoom in? You can't add a tap gesture to MKMapView. That's probably true for the Google map view. – El Tomato Mar 01 '19 at 03:12
  • @ElTomato even if the tap gesture is for a view on the mapview, not the actual mapview itself? – Chris Gong Mar 01 '19 at 03:16
  • As for MKMapView, no. I don't know for GMSMapView since I don't have quick access to a sample project I have made a year ago. – El Tomato Mar 01 '19 at 03:30
  • @ElTomato Just curious, is this from experience or was this documented somewhere? – Chris Gong Mar 01 '19 at 03:39
  • @ElTomato also if that is true that I can't add a tap gesture, then why is `shouldReceive` being called? – Chris Gong Mar 01 '19 at 04:16
  • That's from my experience for macOS. I don't remember doing it for iOS. I don't know about shouldReceive. – El Tomato Mar 01 '19 at 04:21

1 Answers1

5

Put the following code:

mapView.settings.consumesGesturesInView = false

From the 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.

Chris Gong
  • 8,031
  • 4
  • 30
  • 51
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
  • Thank you so much, this answer worked! Would you mind adding the link to where you found this in your answer? I think it would definitely help me and others. – Chris Gong Mar 01 '19 at 06:09
  • I didn't find it in docs. I'm currently working on a map-based application where I faced the same situation and with some hit and trails, I found this. – Sahil Manchanda Mar 01 '19 at 06:34
  • no problem, I was able to do some searching and found that it came from the google maps ios sdk reference. Added the link to your answer, thanks again! – Chris Gong Mar 01 '19 at 06:51