How to Implement UILongPress Gesture Recognizer in Swift

Posted By : Gunjan Gumber | 12-Feb-2018

In this blog, I have explained how we can implement long press gesture as in many of the iOS apps when its required to perform an action on long press we can implement this long press gesture. 

 

To detect the particular type of gesture, we have UIGestureRecognizer class. A gesture recognizer is an object of the abstract class UIGestureRecognizer.  When there is the requirement to detect gesture in-app, it is very easy to do it using UIGesture Recognizer class. We have many types of gesture like pinch gesture, pan gesture long press gesture, rotation gesture. All the actions for all these different types of gestures can pe performed using UIGesture Recognizer classes.

To detect longpressgesture, UILOngPressGestureRecognizer class has a custom method which is built-in to dectect it on any type of viewor we can say imageview

 

UILongPressGestureRecognizer :-

 

 This type of gesture recognizer is detected when the user touches the screen with one or more fingers for minimum time period and touches does not move for that minimum period. It is for the minimum period of time before the action triggers. The pressing must last long enough in order to be detected. In this, if the finger moves a lot around the pressed points, then the gesture fails. This type of gesture is a continous gesture.

 

How to handle UILongpressgesture  for began and ended state

 

func manageLongPress(_ sender: UILongPressGestureRecognizer?) {
if sender?.state == .ended {
 print("+++++ UILongPressGestureRecognizer state is ended +++++")
 // We can write our code here what we want to do at end of gesture
 } else if sender?.state == .began { 
print("++++++ UILongPressGestureRecognizer state has begun ++++.") 
// We can write our code here what we want to do at end of gesture 
} }

Here below I have explained how to zoom in and zoom out of the image view using UILOngPressGestureRecognizer.

Step 1. Create new Xcode project

Step 2. Open ViewController.swift file and create and add an Imageview.

let imageView = UIImageView()

Step 3. Add the image view as the subview. Either do it programatically or from storyboard.

Step 4. Now create an instance to UILongPressGestureRecognizer

var longPressGesture = UILongPressGestureRecognizer()

Now add LongPressGesture to an image view

longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGestureHandlerMethod))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(longPress)

 

Step 5. The last step is to add the action for the LongPressGesture.

 

func longPressGestureHandlerMethod(recognizer:UIPinchGestureRecognizer){
    switch recognizer.state {
    case .began:
        UIView.animate(withDuration: 0.05,
                       animations: {
                        self.imageView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
        },
                       completion: nil)
    case .ended:
        UIView.animate(withDuration: 0.05) {
            self.imageView.transform = CGAffineTransform.identity
        }
    default: break
    }
}

Thanks.

About Author

Author Image
Gunjan Gumber

Gunjan is a bright IOS developer with good knowledge in Swift ,Json, quite good at building user interface.

Request for Proposal

Name is required

Comment is required

Sending message..