计算UIScrollView的minimumZoomScale [英] Calculating minimumZoomScale of a UIScrollView

查看:578
本文介绍了计算UIScrollView的minimumZoomScale的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要将图像加载到图像视图中并将minimumZoomScale和zoomScale设置为aspectFill,比如我计算的比例,如下所示:

I have an image that I want to load into an image view and set the minimumZoomScale, as well as the zoomScale to an aspectFill like scale that I calculate as follows:

// configure the map image scroll view
iImageSize = CGSizeMake(iImageView.bounds.size.width, iImageView.bounds.size.height);
iScrollView.minimumZoomScale = iScrollView.bounds.size.height / iImageSize.height;
iScrollView.maximumZoomScale = 2;
iScrollView.zoomScale = iScrollView.minimumZoomScale;
iScrollView.contentOffset = CGPointMake(0, 0);
iScrollView.clipsToBounds = YES;

iScrollView尺寸为450 x 320px,iImageSize为1600 x 1960px。

The iScrollView size is 450 x 320px and the iImageSize is 1600 x 1960px.

手动执行minimumZoomScale数学:450/1960 = 0.22959184。
系统确定0.234693885而不是(???)。

Doing the minimumZoomScale math by hand: 450 / 1960 = 0.22959184. The system determines 0.234693885 instead (???).

但要使窗口适合450px空间,两个数字都不起作用(!! !)。
我手动尝试,发现0.207是正确的数字(这将转换为2174 xp的图像高度或者UIScrollView高度为406px)。

But to get the window fit into the 450px space, both figures don't work (!!!). I manually tried and found 0.207 is the right number (that would translate to a image height of 2174 xp or alternatively a UIScrollView height of 406px).

For信息:UIScrollview屏幕是450px,即480px减去状态栏高度(10),减去UITabBar高度(20)

For information: the UIScrollview screen is 450px, namely 480px minus status bar height (10), minus UITabBar height (20)

这个不当行为的任何线索?

Any clues on this misbehaviour?

推荐答案

不确定你是否还有这个问题,但对我而言,规模恰恰相反。 minimumZoomScale = 1 对我来说,我必须计算 maximumZoomScale

Not sure if you still have this problem, but for me, the scale is exactly the opposite. minimumZoomScale = 1 for me and I have to calculate the maximumZoomScale.

这是代码:

[self.imageView setImage:image];
// Makes the content size the same size as the imageView size.
// Since the image size and the scroll view size should be the same, the scroll view shouldn't scroll, only bounce.
self.scrollView.contentSize = self.imageView.frame.size;

// despite what tutorials say, the scale actually goes from one (image sized to fit screen) to max (image at actual resolution)
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat minScale = 1;
// max is calculated by finding the max ratio factor of the image size to the scroll view size (which will change based on the device)
CGFloat scaleWidth = image.size.width / scrollViewFrame.size.width;
CGFloat scaleHeight = image.size.height / scrollViewFrame.size.height;
self.scrollView.maximumZoomScale = MAX(scaleWidth, scaleHeight);
self.scrollView.minimumZoomScale = minScale;
// ensure we are zoomed out fully
self.scrollView.zoomScale = minScale;

这篇关于计算UIScrollView的minimumZoomScale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆