UILongPressGestureRecognizer Tutorial in Swift 3.0

A Gesture Recognizing is one type of touch behaviour on screen of user. By use of that we can finding which type of event on screen. user can do on screen like single tap , double tap , rotate his/her finger on screen , Drag finger , Pinch with finger , Swipe etc. 

There are Six types of Gesture Recognises :


 UITapGestureRecognizer
 UIPinchRecognizer
 UIRotationGestureRecognizer
 UISwipeGestureRecognizer
 UIPanGestureRecognizer
 UILongPressGestureRecognizer




This is tutorial only for UILongPressGestureRecognizer.

Follow the below steps :

Step 1 :  Create Project with File > New > Project > TapGesture-Swift3.

Step 2 :  Adding a simple View on Storyboard. and also give IBOutlet connection of the view.
@IBOutlet weak var viewLong: UIView!

Step 3 :  Make an instance of the UILongPressGestureRecognizer.
var longGesture = UILongPressGestureRecognizer()
Step 4 :  Initialise the UILongPressGestureRecognizer and adding long gesture in view. So add the code in ViewDidLoad.
override func viewDidLoad() {
    super.viewDidLoad()
    // LongPress Gesture
    longGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(_:)))
    longGesture.minimumPressDuration = 1
    viewLong.addGestureRecognizer(longGesture)
}
Step 5 :  Method of the LongPressGesture.
func longPress(_ sender: UILongPressGestureRecognizer) {
    let alertC = UIAlertController(title: "Long Press", message: "Long press gesture called when you press on view of 1 second duration.", preferredStyle: UIAlertControllerStyle.alert)
    let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (alert) in
    }
    alertC.addAction(ok)
    self.present(alertC, animated: true, completion: nil)
}
Step 6 :  Output.






Step 7 : 
 Download UILongPressGestureRecognizer Demo

Thanks.


UILongPressGestureRecognizer Tutorial in Swift 3.0 UILongPressGestureRecognizer Tutorial in Swift 3.0 Reviewed by KIRIT MODI on 22:43:00 Rating: 5

No comments:

Powered by Blogger.