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.
  1. UITapGestureRecognizer
  2. UIPinchGestureRecognizer
  3. UIRotationGestureRecognizer
  4. UISwipeGestureRecognizer
  5. UIPanGestureRecognizer
  6. 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
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)
        }
    }
}
 

About Author

Author Image
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.

Request for Proposal

Name is required

Comment is required

Sending message..