简体   繁体   中英

Swift 3 MKMapView - Add pin/annotation on long press

I am trying to set up my MKMapView so that when I press and hold the map for 2 seconds, a pin will appear at the user's current location.

Here is what I have in my viewDidLoad :

let gestureRecognizer = UILongPressGestureRecognizer(target: self, action:(Selector(("longPress:"))))
gestureRecognizer.minimumPressDuration = 2.0
gestureRecognizer.delegate = self
map.addGestureRecognizer(gestureRecognizer)

Then at the bottom of my ViewController class I have the following:

func longPress(gestureRecognizer: UILongPressGestureRecognizer) {

    let coordinate = map.centerCoordinate

    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    map.addAnnotation(annotation)
}

When I run the app and press on the map for 2 seconds, the app crashes. The console says

terminating with uncaught exception of type NSException

and

unrecognized selector sent to instance 0x7f89f0c02ad0

and I am not sure why. Thank you for any guidance you can offer.

Never say

Selector(("longPress:"))

Say

#selector(longPress)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM