Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a long press in Swift?

Tags:

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") 

}
like image 593
user2428168 Avatar asked Jul 10 '14 16:07

user2428168


1 Answers

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:")
like image 100
Mick MacCallum Avatar answered Dec 20 '22 16:12

Mick MacCallum