【问题标题】:iOS UIScrollView pinch zoom adds white spaceiOS UIScrollView捏缩放添加空白
【发布时间】:2014-06-30 09:22:24
【问题描述】:

我正在使用 UIScrollView 中的 UIImageView 来实现缩放以缩放图像。滚动视图不采用全屏尺寸。我将滚动视图的背景设为绿色。然后我将图像放大。

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

这是截图:

这是我的代码:

// 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。

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

【问题讨论】:

  • 移除委托方法 scrollViewDidEndZooming 并再次检查。
  • 删除 scrollViewDidEndZooming 并不能解决放大问题。此外,如果没有该代码,图像在缩小时也不会居中。

标签: ios objective-c uiscrollview uiimageview pinchzoom


【解决方案1】:

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

https://github.com/akhiljayaram/PJImageZoomView

别忘了添加

self.automaticallyAdjustsScrollViewInsets = NO;

在您的视图控制器中。
希望这可以帮助! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 2015-04-25
    • 2011-06-16
    • 2017-09-23
    • 2014-02-16
    相关资源
    最近更新 更多