2
  1. 当操作正在进行时使用 UIPinchGestureRecognizer 缩放图像视图时,图像“尝试”保持在初始 frame.origin。

  2. 即使在平移手势将图像移动到另一个位置之后,捏合也会立即将其移回初始位置。

  3. 演示手势的 Apple Touches 示例应用程序的公然副本的所有代码。

下面是捏代码,我已经在 github 上推送了一个示例应用程序来展示这种行为:https ://github.com/atokubi/TestImageManipulation

提前感谢你的帮助。

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

1 回答 1

1

如果要正确重新居中图像,可以使用此方法:

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

在您的“adjustAnchorPointForGestureRecognizer”中添加此方法,方法如下:

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

我下载了你的代码并实现了这个。有用。

如果有帮助,请告诉我。

于 2014-02-13T20:22:40.697 回答