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

Ios 设置UIScrollView缩放比例以适应屏幕,ios,objective-c,iphone,uiscrollview,uiimageview,Ios,Objective C,Iphone,Uiscrollview,Uiimageview,我的观点是复制许多使用照片的应用程序的行为,当uicrollview与UIImageView一起显示时,即使图像较小,也会使图像适合屏幕大小 我确信这不应该用UIImageView框架来完成,而应该由UIScrollView来操作,但是如何 在我的代码中,我有一个名为updateZoom的方法,该方法计算minimumZoomScale值,并将该值分配给zoomScale属性。我想这是做数学和手工计算的正确地方 方法如下: - (void)updateZoom { float minZ

我的观点是复制许多使用照片的应用程序的行为,当
uicrollview
UIImageView
一起显示时,即使图像较小,也会使图像适合屏幕大小

我确信这不应该用
UIImageView
框架来完成,而应该由
UIScrollView
来操作,但是如何


在我的代码中,我有一个名为
updateZoom
的方法,该方法计算
minimumZoomScale
值,并将该值分配给
zoomScale
属性。我想这是做数学和手工计算的正确地方

方法如下:

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


我希望有人能告诉我如何设置
zoomScale
以适应
UIScrollView
的边界。

要设置适合scrollView中图像大小的缩放,请使用以下方法设置scrollView的最小缩放:

self.scrollView.minimumZoomScale=self.scrollView.frame.size.width/self.imageView.frame.size.width;


以上代码将使您的图像适合滚动视图宽度

解决方案相当简单。。。检查此代码(OP中方法的修改版本):


我认为没有什么需要解释的,因为代码是直截了当的。

上面的解决方案是可行的,但我很难理解为什么。我想与大家分享这两篇文章,我发现这两篇文章对正确设置mininumZoomScale以及将其与iOS8/XCode 6配合使用非常有帮助

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

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

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

非常感谢机器人娜塔莎的约束解决方案:

当调用updateZoom方法调整最小值时,还应检查当前zoomScale是否<你的最小zoomScale,如果是,则重置它。否则不应重置zoomScale,因为当方向发生变化时,这将成为用户的另一种提问方式

至于它的其余部分以及数学原理,objc.io的Joe Conway在这篇关于bounds vs.frame with UIScrollView的精彩文章中阐述了这一切:

多亏了这两个和上面的海报

-(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;
}
-(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
这是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

SWIFT 3

感谢@chewy,这对我来说很有效(我需要在图片中添加“边界”):


然后在“override func viewWillLayoutSubviews()”中调用setZoomSale()

我的情况不一样,但您的想法帮助我谢谢
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
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;

}