Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to rotate and zoom a view using UIGestureRecognizer

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 652
    Comment on it

    Rotating and zooming a view is a pretty simple task. This blog shows how rotate and zoom a view by implementing UIPinchGestureRecognizer and UIRotateGestureRecognizer.

      1. Add pinch and rotate gesture recognizers to the target view. (Gesture recognizers can also be added on xib) imageView is the target view in this example.
      UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
          [pinch setDelegate:self];
          [imageView addGestureRecognizer:pinch];
      
          UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotate:)];
          [rotate setDelegate:self];
          [imageView addGestureRecognizer:rotate];
      
      2. Handle pinch and rotate
      -(void)handlePinch:(UIPinchGestureRecognizer*)pinchRecognizer {
          pinchRecognizer.view.transform = CGAffineTransformScale(pinchRecognizer.view.transform, pinchRecognizer.scale, pinchRecognizer.scale);
          pinchRecognizer.scale = 1;
      }
      
      - (IBAction)handleRotate:(UIRotationGestureRecognizer *)rotateRecognizer {
          rotateRecognizer.view.transform = CGAffineTransformRotate(rotateRecognizer.view.transform, rotateRecognizer.rotation);
          rotateRecognizer.rotation = 0;
      }
      
      3. To make the rotate and pan gesture work simultaneously we need to implement the following delegate method
      - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
          return YES;
      }
      

    NOTE : Enabling auto-layout might result in improper functioning.

    References:

    A better tutorial of the same and added features are present on this tutorial of RayWenderlich.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: