UI Gesture Recognization in swift
Posted By : Manu Gupta | 30-Jun-2015
UIGestureRecognizer is an abstract base class for concrete gesture-recognizer classes. A gesture-recognizer is simply an object that is used to recognizing a gesture and acting on that recognition. When an object recognizes a gesture, it sends an action message to target object.
There are many types of gesture recognizers.
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UILongPressGestureRecognizer
UITapGestureRecognizer is a subclass of UIGestureRecognizer that is used to recognised gesture on single tap or multiple taps. In this we can also specified no of fingres tapped or how many times fingers tapped.
UIPinchGestureRecognizer is the pinching gesture that involved two touches. This is an continuos gesture used while pinching two fingers. It is mainly used in case of image zooming and zoom out.
UIRotationGestureRecognizer is the gesture that involve two touches. This guesture is called when some one rotate their two fingers in circular motion. This gesture returns speed and velocity of rotation.
UISwipeGestureRecognizer is the gesture that is called when user swipe the finger in one or more direction.
UIPanGestureRecognizer is the gesture used to dragging an object from one position to another.
UILongPressGestureRecognizer is the gesture that is called on long pressing an object. This gesture is called when user hold an object for some minimum duration of time.
Example:
To add an long press gesture to an image view
To add an long press gesture to an image view
func addLongPressGesture(imageView: UIImageView, selector: String){ let longPressRecMainProfile = UILongPressGestureRecognizer(target: self, action: Selector(selector)) imageView.addGestureRecognizer(longPressRecMainProfile) }
To remove all gestures from image view.
func removeLongPressGesture(imageView: UIImageView, selector: String){ if let recognizers = imageView.gestureRecognizers { for recognizer in recognizers { imageView.removeGestureRecognizer(recognizer as! UIGestureRecognizer) } } }
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manu Gupta
Manu is a Native iOS (Swift) and Titanium lead developer . Manu has good experience working with Swift and Alloy framework as well. He loves watching movies and travelling.