ios 设置 UIScrollView 缩放比例以适应屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25927402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:36:59  来源:igfitidea点击:

Setting UIScrollView zoom scale to fit the screen

iosobjective-ciphoneuiscrollviewuiimageview

提问by cojoj

My point is to copy the behaviour of many apps which uses photos where when UIScrollViewwith UIImageViewinside is being presented it fits image to the size of screen even if this image is smaller.

我的观点是复制许多使用照片的应用程序的行为,当内部呈现时UIScrollViewUIImageView即使此图像较小,它也会使图像适合屏幕大小。

I'm certain that this shouldn't be done with UIImageViewframe but manipulated by UIScrollViewbut how...?

我确信这不应该用UIImageView框架来完成,而是由UIScrollView……来操纵?



In my code I have a method called updateZoomwhich calculates the minimumZoomScalevalue and also assigns this value to the zoomScaleproperty. I guess that this is the right place to do math and calculate zoomScalemanually.

在我的代码中,我调用了一个方法updateZoom来计算minimumZoomScale值并将该值分配给zoomScale属性。我想这是进行数学和zoomScale手动计算的正确位置。

This is the method:

这是方法:

- (void)updateZoom {
    float minZoom = MIN(self.view.bounds.size.width / self.imageView.image.size.width, self.view.bounds.size.height / self.imageView.image.size.height);

    if (minZoom > 1) {
        minZoom = 1;
    }

    self.scrollView.minimumZoomScale = minZoom;
    self.scrollView.zoomScale = minZoom;
}


I hope that someone would give me a hit on how to set the zoomScaleto fit the UIScrollViewbounds.

我希望有人能给我一个关于如何设置zoomScale以适应UIScrollView界限的机会。

回答by Dan Loughney

The solutions above work, but I was struggling to understand why. I would like to share these two articles that I found extremely helpful in setting the mininumZoomScale correctly and getting this to work with iOS8/XCode 6.

上述解决方案有效,但我很难理解为什么。我想分享这两篇文章,我发现它们对正确设置 mininumZoomScale 并使其与 iOS8/XCode 6 配合使用非常有帮助。

First, getting the scroll view to work on iOS8 demands that you set your constraints correctly from the start. Other posters have recommended, and I agree, that your scroll view should only ever have a single content view. Fine to embed all of your other views within the one content view, but you don't want to set your scroll view constraints to a bunch of little views.

首先,让滚动视图在 iOS8 上工作需要你从一开始就正确设置约束。其他海报建议,我同意,您的滚动视图应该只有一个内容视图。可以将所有其他视图嵌入到一个内容视图中,但您不想将滚动视图约束设置为一堆小视图。

Connect your Top, Leading, Trailing, and Bottom constraints to the scroll view from the content view (in my case it is an UIImageView) with constant 0. This sets the edges of your content view to the scroll view.

将顶部、前导、尾随和底部约束从内容视图(在我的例子中是 UIImageView)与常量 0 连接到滚动视图。这将内容视图的边缘设置为滚动视图。

Next, set the size of the content view using the Equal Width and Equal Height constraints to the scroll view. You can't use Fixed Width and Fixed Height in the content view because zooming won't work, nor can you manage the size yourself. Without these constraints, iOS8 sets the size to (0,0) before you have a chance resulting in some odd artifacts. With these constraints, call your updateZoom method in the orientation method and you are good. In iOS7 call it in didRotateFromInterfaceOrientation. In iOS8 call it from viewWillTransitionToSize. You'll need to update the code to pass in the new size.

接下来,使用 Equal Width 和 Equal Height 约束为滚动视图设置内容视图的大小。你不能在内容视图中使用固定宽度和固定高度,因为缩放不起作用,你也不能自己管理大小。如果没有这些限制,iOS8 会在您有机会导致一些奇怪的工件之前将大小设置为 (0,0)。有了这些约束,在方向方法中调用你的 updateZoom 方法,你就很好了。在 iOS7 中,在 didRotateFromInterfaceOrientation 中调用它。在 iOS8 中从 viewWillTransitionToSize 调用它。您需要更新代码以传入新的大小。

Many thanks to Natasha The Robot for the constraint solution: http://natashatherobot.com/ios-autolayout-scrollview/

非常感谢 Natasha The Robot 的约束解决方案:http: //natashatherobot.com/ios-autolayout-scrollview/

When you call your updateZoom method to tweak your minimum, you should also check whether your current zoomScale is < your minimumZoomScale and reset it if it is. You should not reset the zoomScale otherwise as this becomes another quizzical look from your user when orientation changes.

当您调用 updateZoom 方法来调整您的最小值时,您还应该检查您当前的 zoomScale 是否小于您的 minimumZoomScale,如果是,则将其重置。您不应该重置 zoomScale 否则,当方向改变时,这会成为用户的另一种古怪外观。

As for the rest of it and why the math works, Joe Conway at objc.io laid it all out in this fantastic article on bounds vs. frame with UIScrollViews: http://www.objc.io/issue-3/scroll-view.html

至于它的其余部分以及为什么数学有效,objc.io 的 Joe Conway 在这篇关于使用 UIScrollViews 的边界与框架的精彩文章中阐述了所有内容:http://www.objc.io/issue-3/scroll-视图.html

Thanks to both and the poster's above.

感谢上面的两个和海报。

-(void)updateZoom {
    self.scrollView.minimumZoomScale = MIN(self.scrollView.bounds.size.width / self.imageView.image.size.width, self.scrollView.bounds.size.height / self.imageView.image.size.height);

    if (self.scrollView.zoomScale < self.scrollView.minimumZoomScale)
        self.scrollView.zoomScale = self.scrollView.minimumZoomScale;
}

回答by cojoj

The solution was fairly easy... Check out this code (modified version of method from OP):

解决方案相当简单......查看此代码(来自OP的方法的修改版本):

- (void)updateZoom {
    float zoomScale = MIN(self.view.bounds.size.width / self.imageView.image.size.width, self.view.bounds.size.height / self.imageView.image.size.height);

    if (zoomScale > 1) {
        self.scrollView.minimumZoomScale = 1;
    }

    self.scrollView.minimumZoomScale = zoomScale;
    self.scrollView.zoomScale = zoomScale;
}

I think there is nothing to explain as the code is straight forward.

我认为没有什么可解释的,因为代码很简单。

回答by A.J. Hernandez

SWIFT 3

斯威夫特 3

Thanks to @chewy this worked for me (I needed to add "bounds" to the image):

感谢@chewy,这对我有用(我需要为图像添加“边界”):

func setZoomScale() {

    var minZoom = min(self.view.bounds.size.width / imageView!.bounds.size.width, self.view.bounds.size.height / imageView!.bounds.size.height);

    if (minZoom > 1.0) {
        minZoom = 1.0;
    }

    scrollView.minimumZoomScale = minZoom;
    scrollView.zoomScale = minZoom;

}

Then call setZoomScale() in "override func viewWillLayoutSubviews()"

然后在“覆盖 func viewWillLayoutSubviews()”中调用 setZoomScale()

回答by Aks

To set the zoom which fits the image size in scrollView set the the minimumZoom for scrollView using this : self.scrollView.minimumZoomScale = self.scrollView.frame.size.width/ self.imageView.frame.size.width;

要在 scrollView 中设置适合图像大小的缩放,请使用以下命令设置 scrollView 的 minimumZoom: self.scrollView.minimumZoomScale = self.scrollView.frame.size.width/ self.imageView.frame.size.width;

Above code will fit your image in the scrollView Width.

上面的代码将适合您在 scrollView 宽度中的图像。

回答by chewy

And here is the Swift 3 version

这是 Swift 3 版本

var minZoom = min(self.view.bounds.size.width / theImage!.size.width, self.view.bounds.size.height / theImage!.size.height)
if minZoom > 1.0 {
    minZoom = 1.0
}

self.scrollImg.minimumZoomScale = minZoom
self.scrollImg.zoomScale = minZoom