lithium
lithium

Reputation: 1302

UITapGestureRecognizer is not working with Google Maps SDK

I have a mapViewController with some custom UIView over it (just two labels with some background). I want to show new ViewController after the tap on this UIView.

So, my code (this was a working solution for other cases) for custom UIView:

 tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "checkGesture:")
        tapGestureRecognizer.numberOfTapsRequired = 1
        self.userInteractionEnabled = true
        self.addGestureRecognizer(tapGestureRecognizer)

and

func checkGesture(sender: UITapGestureRecognizer) {
    print("it works")
}

Could anyone please tell me what is the problem here?

Edit (Solved): So, I tried to put this custom UIView on another ViewController and it works. It seems that main source of this problem is the next part of code in my MapViewController:

let mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
    self.view = mapView

Without the last line (self.view = mapView), my custom UIView works (but I don't have any map, so I had to change this code slightly).

let mapView = GMSMapView.mapWithFrame(CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), camera:camera)
    self.view.addSubview(mapView)

Upvotes: 4

Views: 1645

Answers (2)

kstefanou
kstefanou

Reputation: 617

I think that best solution if you don't want to add the GMSMapView as a subview is:

  1. Add GMSMapViewDelegate to your ViewController.

  2. On viewDidLoad of your ViewController add myMapView.delegate = self.

  3. Also add the following function:

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        print("!map tapped!")
    }
    

Upvotes: 5

Dennis Weidmann
Dennis Weidmann

Reputation: 1967

I don't know if it is the problem, but change this line

tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "checkGesture:")

to this

tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("checkGesture:"))

and report me if its working, or if there are further problems

Upvotes: 0

Related Questions