2
  1. When scaling an image view using the UIPinchGestureRecognizer when the action is ongoing the image "tries" to remain at the initial frame.origin.

  2. Even after a pan gesture has moved the image to another location a pinch promptly moves it back to the initial location.

  3. All of the code for this blatant copy of Apple's Touches sample app that demonstrates gestures.

Below is the code for pinch, I have pushed a sample app on github demonstrating this behavior: https://github.com/atokubi/TestImageManipulation

Thanks in advance for your assistance.

- (IBAction)scaleItem:(UIPinchGestureRecognizer *)gestureRecognizer {
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
        [gestureRecognizer setScale:1];
    }    
} 

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:piece];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];       
        piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;
    }
}
dotKwame
  • 181
  • 3
  • 13
  • Can you please elaborate the exact issue? If I am getting it correctly, you are not able to properly recentre the image after pinching in or out? Is it the case? – Sudershan shastri Feb 13 '14 at 20:12
  • I have an image view which I have added a pinch and a pan gesture. the pan gesture works ok moving the image freely about but when the image is pinched it "jumps" to the original frame.origin (x, y) that is was attached on the viewcontroller in storyboard. You can clone the git repo i included to check it out. – dotKwame Feb 13 '14 at 20:54

1 Answers1

1

If you want to properly re center the image, you can use this method :

// To re-center the image after zooming in and zooming out
- (void)centerViewContents
 {
   CGSize boundsSize = self.bounds.size;
   CGRect contentsFrame = self.imageView.frame;

   if (contentsFrame.size.width < boundsSize.width)
   {
      contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
   }
   else
   {
   contentsFrame.origin.x = 0.0f;
   }

   if (contentsFrame.size.height < boundsSize.height)
   {
     contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
   }
   else
   {
    contentsFrame.origin.y = 0.0f;
   }

   self.imageView.frame = contentsFrame;
}

Add this method in your "adjustAnchorPointForGestureRecognizer" ,ethod like this:

  - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
   {
     if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
     UIView *piece = gestureRecognizer.view;
     CGPoint locationInView = [gestureRecognizer locationInView:piece];
     CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

     piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
     piece.center = locationInSuperview;
     [self centerViewContents];
     }
  }

I downloaded your code and implemented this. It works.

Please let me know if it helps.

  • sorry no as my question has nothing to do with scrollview. – dotKwame Feb 13 '14 at 20:55
  • Hi. I have gone through your code through GITHUB and edited it. It must work. You just have to place "centerViewContents" method at right place. Please try it and let me know if it works. Thanks :) – Sudershan shastri Feb 14 '14 at 06:07
  • No, I tried but n=same effect as before. I'm beginning to wonder if it might be an ios bug of sorts. – dotKwame Feb 14 '14 at 22:51
  • I'm currently using the PEPhotoCropEditor I found on github which does what I ultimately wanted to do. – dotKwame Feb 16 '14 at 16:42