NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Project 5 (UIKit): NSGestureRecognizer doesn't fire the action (mapClicked)

Forums > macOS

@markr  

Hi! environment is xcode 12.2/Catalina (10.15.7), and I'm trying to get pins to drop on the map at the start of the project (and nothing happens)

Some background: I haven't had to get a developer account - the error and capabilities requirement mentioned on page 254 ("enabling maps") didn't generate an error, and the map renders fine.

Using my relatively amateur debugging skills, it looks like the NSGestureRecognizer registers agains the view fine:

    override func viewDidLoad() {
        super.viewDidLoad()

        let recognizer = NSGestureRecognizer(target: self, action: #selector(mapClicked))
        mapView.addGestureRecognizer(recognizer)
    }

but no gesture is ever recognised (it never calls mapClicked)

    @objc func mapClicked(recognizer: NSClickGestureRecognizer) {
        debugPrint(recognizer)
        let location = recognizer.location(in: mapView)
        let coordinates = mapView.convert(location, toCoordinateFrom: mapView)
        addPin(at: coordinates)
    }

Any thought on whether Is this related to signing/the maps capability? (I wasn't going to spend £79 to find out, but I guess I'm going to need an account at some point :) update: did it, looks like the capabilities UI has changed, but figured out how to add it (plus sign on the top left) and the capability is now listed in the entitlements - still doesn't trigger mapClicked

Is there a more effective way to debug NSGestureRecognizer?

1      

@markr  

solved: NSGestureRecognizer should be NSClickGestureRecognizer

doh :/

1      

Hacking with Swift is sponsored by MadMachine

SPONSORED Want to try Swift on microcontrollers? MadMachine provides ways to interact with the physical world in a Swift way. Join us and have fun!

Get it now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.