简体   繁体   English

iOS UIScrollView捏缩放可添加空白

[英]iOS UIScrollView pinch zoom adds white space

I'm implementing pinch to zoom of an image using UIImageView inside a UIScrollView. 我正在实现使用UIScrollView内的UIImageView缩放图像的功能。 The scroll view doesn't take full screen size. 滚动视图不全屏显示。 I made the background of the scrollview green. 我将滚动视图的背景设置为绿色。 I then pinch-zoomed in the image. 然后,我将图像放大。

The problem is that when I scroll the zoomed image it's not centered correctly in the scroll view and has some space to the right and bottom. 问题是,当我滚动缩放的图像时,它在滚动视图中无法正确居中,并且在右侧和底部都有一些空间。 If I try to scroll to the top or left the scroll view doesn't allow to scroll to the end of the image by similar amount of space. 如果我尝试滚动到顶部或左侧,则滚动视图不允许滚动到图像末尾类似的空间。 The amount of space seems to be related on how I move my fingers when doing the pinch gesture. 空间大小似乎与我在捏手势时如何移动手指有关。

Here is a screenshot: 这是屏幕截图:

滚动视图中的空间为绿色 Here is my code: 这是我的代码:

// 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(@"==================================");
}

And here is my output: 这是我的输出:

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
==================================

I tried setting automaticallyAdjustsScrollViewInsets to NO, but my view controller doesn't respond to the selector. 我尝试将autoAdjustsScrollViewInsets自动设置为NO,但是我的视图控制器未响应选择器。 And besides, the insets report 0-s. 此外,插图报告为0秒。

Any solution or work-around would be great. 任何解决方案或解决方法都很好。 Any idea or possible explanation for this behavior would also be appreciated. 对于这种行为的任何想法或可能的解释也将不胜感激。

I suggest you to use the following library in Github: 我建议您在Github中使用以下库:

https://github.com/akhiljayaram/PJImageZoomView https://github.com/akhiljayaram/PJImageZoomView

Don't forget to add 不要忘记添加

self.automaticallyAdjustsScrollViewInsets = NO;

in your viewcontroller. 在您的ViewController中。
Hope this helps! 希望这可以帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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