-2

I am new to Swift language and I want to implement a swipe gesture to my image so that I can change the page with it. For example when you received a whatsapp notification on any iPhone home screen. But when I am adding swipe gesture programmatically it gives me this error.

let rightSwipe = UISwipeGestureRecognizer(target: swipeImage, action: Selector("handleSwipe:"))

rightSwipe.direction = .Right

view.addGestureRecognizer(rightSwipe)

the error is when I am giving the id of my UIImage named swipeImage it gives me unrecognized selector sent to instance but when I give it named self it doesn't give me an error. But i want to add swipe gesture to my image. I hope I explained my problem.

Edit 1: Eventhough i changed the target to self.swipeImage it gives me the same error. Also I added addGestureRecognizer to my image by changing view to swipeImage but it does not work too

Thanks.

1 Answers1

0

The target is the object wants to have the action called on it when the swipe happens.

It is not possible to add a gesture recognizer to a UIImage. They need to be added to UIView subclasses like UIImageView. You just need to use the addGestureRecognizer: method on the image view to add it.

let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe:"))

imageView.addGestureRecognizer(rightSwipe)
Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • I did it but it does not take the action at all. I put a label and changing it when the swipe method called and when I am trying to swipe right my label does not change. By the way thanks for giving an answer – Burak Gumus Mar 23 '16 at 16:22
  • I forgot to make it user interaction enabled. Thanks for the answer. – Burak Gumus Mar 23 '16 at 16:40