0

In my application for zooming and panning, i'm using above said gesture recognizers. This is working fine. I want to a button which will bring back the image to initial state. That means show the actual image or reset to initial state. Can some one tell me how to achieve this?

The code is as below:

-(void)handlePanGesture:(UIPanGestureRecognizer*)recognizer

{
    CGPoint translation = [(UIPanGestureRecognizer*)recognizer translationInView:[self superview]];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
    [(UIPanGestureRecognizer*)recognizer setTranslation:CGPointMake(0, 0) inView:[self superview]];
}

-(void)handlePinchGesture:(UIPinchGestureRecognizer*)recognizer
{
    static CGRect initialBounds;

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        initialBounds = self.bounds;
    }
    CGFloat factor = [(UIPinchGestureRecognizer *)recognizer scale];

    CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
    self.bounds = CGRectApplyAffineTransform(initialBounds, zt);
}
Satyam
  • 15,493
  • 31
  • 131
  • 244
  • Set the image's center back to its original, and set the transform to `CGAffineTransformIdentity` and you're done. Or is there some other problem you are not mentioning? – borrrden Jun 20 '13 at 03:47
  • Actually you are doing this in a weird way. Why are you using ApplyTransform instead of just setting the transform property of the image? – borrrden Jun 20 '13 at 03:48
  • @borrrden, can you please provide me the actual code to implement it. – Satyam Jun 20 '13 at 04:12
  • 1
    No, I refuse. I am not in the habit of providing code samples. You can accomplish this yourself. There are fine tutorials on the Internet such as this one: http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more – borrrden Jun 20 '13 at 04:56
  • @borrden, I figured out the problem. Thanks for your suggestion. – Satyam Jun 29 '13 at 07:32

1 Answers1

0

Based on @borrden's comment.

  1. Check if the current center of the ImageView and original center are same. If not reset the center of the ImageView. You can add a UIView.animation.. to make it look good.
  2. Resize the imageView to original size by setting it to CGAffineTransformIdentity. This can also be added to the UIView.animation.. in the above.
  3. Code. Make changes as per your need.

    UIView.animateWithDuration(0.2, delay: 0.0, options: .CurveEaseIn, animations: {
    
          //Move image back to center
          self.mainImageView.center = self.originalCenter!
          self.layoutIfNeeded()
    
          //Resize image to original
          self.mainImageView.transform = CGAffineTransformIdentity
    
          }, completion: nil
    )
    
Vipin Johney
  • 1,315
  • 14
  • 21