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
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
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.
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
Gunjan Gumber
Gunjan is a bright IOS developer with good knowledge in Swift ,Json, quite good at building user interface.