iOS UIScrollView捏缩放增加了空白区域

时间:2014-06-30 09:22:25

标签: ios objective-c uiscrollview uiimageview pinchzoom

我正在UIScrollView中使用UIImageView实现缩放图像。滚动视图不占全屏尺寸。我将scrollview的背景设为绿色。然后我捏了一下图像。

问题是当我滚动缩放的图像时,它在滚动视图中没有正确居中,并且在右侧和底部有一些空间。如果我尝试滚动到顶部或左侧,滚动视图不允许以相似的空间滚动到图像的末尾。在进行捏合手势时,空间量似乎与我移动手指的方式有关。

以下是截图:

space in scrollview is green 这是我的代码:

// On viewWillAppear:
- (void)loadMyImage
{
    self.myImage.contentMode = UIViewContentModeScaleAspectFit;
    self.myImage.image = self.originalImage; // an UIImage loaded from gallery

    self.myImage.bounds = CGRectMake(0, 0, self.originalImage.size.width, self.originalImage.size.height);
    self.scrollView.bounds = CGRectMake(0,0,320,200);
    self.scrollView.frame = CGRectMake(0,0,320,200);
    self.scrollView.minimumZoomScale=1.0f;
    self.scrollView.maximumZoomScale=2.0f;
    self.scrollView.delegate = self;

    self.myImage.userInteractionEnabled = YES;
}

// Scroll view zooming events:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{return self.myImage;}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.myImage.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.myImage.center = CGPointMake(
        contentsFrame.origin.x + contentsFrame.size.width / 2.0f,
        contentsFrame.origin.y + contentsFrame.size.height / 2.0f);

    CGPoint offset = CGPointMake(
        self.myImage.center.x - self.scrollView.bounds.size.width / 2.0f,
        self.myImage.center.y - self.scrollView.bounds.size.height / 2.0f);

    [UIView animateWithDuration:.25 animations:^{
        [self.scrollView setContentOffset:offset];
    }];

    NSLog(@"Final state of frame: %g x %g (%g , %g) and center (%g , %g)",
          self.myImage.frame.size.width, self.myImage.frame.size.height,
          self.myImage.frame.origin.x, self.myImage.frame.origin.y,
          self.myImage.center.x, self.myImage.center.y);
    NSLog(@"offset: (%g , %g)",
          self.scrollView.contentOffset.x, self.scrollView.contentOffset.y);
    NSLog(@"Inset left %g , right %g , top %g , bottom %g",
          self.scrollView.contentInset.left,
          self.scrollView.contentInset.right,
          self.scrollView.contentInset.top,
          self.scrollView.contentInset.bottom);
    NSLog(@"==================================");
}

这是我的输出:

Initial state of bounds: 320 x 200
of frame: 320 x 200 (0 , 0) and center (160 , 100)
offset: (0 , 0)
==================================
Final state of frame: 640 x 400 (0 , 0) and center (320 , 200)
offset: (160 , 100)
Inset left 0 , right 0 , top 0 , bottom 0
==================================

我尝试将automaticAdjustsScrollViewInsets设置为NO,但我的视图控制器不响应选择器。此外,插图报告0-s。

任何解决方案或解决方案都会很棒。对此行为的任何想法或可能的解释也将不胜感激。

1 个答案:

答案 0 :(得分:3)

我建议你在Github中使用以下库:

https://github.com/akhiljayaram/PJImageZoomView

别忘了添加

self.automaticallyAdjustsScrollViewInsets = NO;
在你的viewcontroller中

希望这可以帮助! :)