简体   繁体   中英

on pinch, image view shifts back to original frame's origin even after pan to different location

  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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM