2014-09-19 95 views
7

我的观点是复制许多使用照片的应用程序的行为,其中UIScrollViewUIImageView内部正在呈现它适合图像的大小,即使此图像较小。设置UIScrollView缩放比例以适合屏幕

我敢肯定,这不应该与UIImageView框架,但UIScrollView操纵,但如何......?


在我的代码我有一个名为updateZoom方法,其计算minimumZoomScale值,也将此值赋给了zoomScale财产。我想这是做数学计算的好地方,并且手动计算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界限。

回答

2

要设置其适合在滚动视图图像大小使用该设置的minimumZoom了滚动缩放: self.scrollView.minimumZoomScale = self.scrollView.frame.size.width/ self.imageView.frame.size.width;

上面的代码将适合在滚动视图宽度图像。

5

的解决方案是相当容易......看看这个代码(由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; 
} 

我认为没有什么解释的代码是直线前进。

10

上述解决方案的工作,但我努力理解为什么。我想分享这两篇文章,我发现这篇文章对于正确设置mininumZoomScale以及使其与iOS8/XCode 6一起工作非常有帮助。

首先,让滚动视图在iOS8上工作要求您正确设置约束从头开始。其他海报推荐,我同意,你的滚动视图应该只有一个单一的内容视图。很好地将所有其他视图嵌入到一个内容视图中,但您不希望将滚动视图约束设置为一堆小视图。

将你的Top,Leading,Trailing和Bottom约束连接到内容视图的滚动视图(在我的情况下,它是一个UIImageView),其常量为0.这将内容视图的边缘设置为滚动视图。

接下来,使用Equal Width和Equal Height约束将内容视图的大小设置为滚动视图。您无法在内容视图中使用“固定宽度”和“固定高度”,因为缩放功能无效,您也无法自行管理大小。如果没有这些约束,iOS8会将大小设置为(0,0),然后才有可能导致一些奇怪的工件。有了这些约束,请在orientation方法中调用updateZoom方法,这很好。在iOS7中,在didRotateFromInterfaceOrientation中调用它。在iOS8中,从viewWillTransitionToSize中调用它。您需要更新代码以传递新的尺寸。

非常感谢娜塔莎的约束求解机器人:http://natashatherobot.com/ios-autolayout-scrollview/

当你打电话给你updateZoom方法来调整你的最低限度,你也应该检查你当前zoomScale是否<您minimumZoomScale并重置它,如果它是。您不应该重置zoomScale,否则在方向更改时,这会成为用户的另一个令人疑惑的外观。

至于它的其余部分,为什么数学著作,乔·康威在objc.io与UIScrollViews奠定这一切了这个梦幻般的文章中就边界与框架:http://www.objc.io/issue-3/scroll-view.html

由于双方和海报的上方。

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

这里是斯威夫特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; 
1

SWIFT 3

由于@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; 

} 

然后在“override func viewWil”中调用setZoomScale() lLayoutSubviews()“