user2428168
user2428168

Reputation: 337

How to do a long press in Swift?

I'm trying to implement a long press on a mapView in Swift (to achieve this)

I don't get any compiler errors but when I do the longpress in the simulator the app crashes with "unrecognized selector sent to instance"

I suspect it's something to do with selectors (similar to this) but every combination I've tried fails

I have this in viewDidLoad:

var lpgr = UILongPressGestureRecognizer(target: self, action: "action") 

lpgr.minimumPressDuration = 2.0; 

mapView.addGestureRecognizer(lpgr)

and this in the ViewController class:

func action(gestureRecognizer:UIGestureRecognizer) { 

println("long press") 

}

Upvotes: 9

Views: 7514

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130193

The method signature of the method:

func action(gestureRecognizer:UIGestureRecognizer) { }

Needs to include a colon for its parameter. You should be using this.

var lpgr = UILongPressGestureRecognizer(target: self, action: "action:")

Upvotes: 16

Related Questions