UIPanGestureRecognizer 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
 UIPinchGestureRecognizer
 UIRotationGestureRecognizer
 UISwipeGestureRecognizer
 UIPanGestureRecognizer
 UILongPressGestureRecognizer





This is tutorial only for UIPanGestureRecognizer.

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 viewDrag: UIView!
Step 3 :  Make an instance of the UIPanGestureRecognizer.
var panGesture  = UIPanGestureRecognizer()
Step 4 :  Initialise the UIPanGestureRecognizer and adding pan gesture in view. Adding below code in ViewDidLoad.
override func viewDidLoad() {
    super.viewDidLoad()
    panGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.draggedView(_:)))
    viewDrag.isUserInteractionEnabled = true
    viewDrag.addGestureRecognizer(panGesture)
}
Step 5 :  Method of the PanGesture.
func draggedView(_ sender:UIPanGestureRecognizer){
    self.view.bringSubview(toFront: viewDrag)
    let translation = sender.translation(in: self.view)
    viewDrag.center = CGPoint(x: viewDrag.center.x + translation.x, y: viewDrag.center.y + translation.y)
    sender.setTranslation(CGPoint.zero, in: self.view)
}
Step 6 :  Output.

Step 7 :  Download UIPanGestureRecognizer Demo


Thanks.
UIPanGestureRecognizer Tutorial in Swift 3.0 UIPanGestureRecognizer Tutorial in Swift 3.0 Reviewed by KIRIT MODI on 00:52:00 Rating: 5

No comments:

Powered by Blogger.