UIRotationGestureRecognizer 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 UIRotationGestureRecognizer.

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 viewRotate: UIView!
Step 3 :  Make an instance of the UIRotationGestureRecognizer. and also declare rotation constant as float value.
var rotateGesture  = UIRotationGestureRecognizer()
var lastRotation   = CGFloat()
Step 4 :  Initialise the UIRotationGestureRecognizer and adding pan gesture in view. Adding below code in ViewDidLoad.
override func viewDidLoad() {
    super.viewDidLoad()
    //ROTATE Gesture
    rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.rotatedView(_:)))
    viewRotate.addGestureRecognizer(rotateGesture)
    viewRotate.isUserInteractionEnabled = true
    viewRotate.isMultipleTouchEnabled = true
}
Step 5 :  Method of the RotationGesture.

func rotatedView(_ sender : UIRotationGestureRecognizer){
    var lastRotation = CGFloat()
    self.view.bringSubview(toFront: viewRotate)
    if(sender.state == UIGestureRecognizerState.ended){
        lastRotation = 0.0;
    }
    let rotation = 0.0 - (lastRotation - sender.rotation)
   // var point = rotateGesture.location(in: viewRotate)
    let currentTrans = sender.view?.transform
    let newTrans = currentTrans!.rotated(by: rotation)
    sender.view?.transform = newTrans
    lastRotation = sender.rotation
}

Step 6 :  Output.



Step 7 : 
 Download UIRotationGestureRecognizer Demo



Thanks.
UIRotationGestureRecognizer Tutorial in Swift 3.0 UIRotationGestureRecognizer Tutorial in Swift 3.0 Reviewed by KIRIT MODI on 07:10:00 Rating: 5

No comments:

Powered by Blogger.