首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算UIScrollView的minimumZoomScale

计算UIScrollView的minimumZoomScale
EN

Stack Overflow用户
提问于 2010-10-06 13:47:18
回答 2查看 4.2K关注 0票数 7

我有一个要加载到图像视图中的图像,并将minimumZoomScale和zoomScale设置为类似于aspectFill的比例,我按如下方式计算:

代码语言:javascript
复制
// 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。

手工计算minimumZoomScale : 450 /1960年= 0.22959184。系统会改为确定0.234693885 (?)。

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

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

关于这种不端行为有什么线索吗?

EN

回答 2

Stack Overflow用户

发布于 2015-06-10 02:23:19

我不确定你是否仍然有这个问题,但对我来说,规模正好相反。我需要计算minimumZoomScale = 1,我需要计算maximumZoomScale

代码如下:

代码语言:javascript
复制
[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;
票数 0
EN

Stack Overflow用户

发布于 2010-10-06 18:39:31

对于简单的放大/缩小,我通常使用以下代码。minimumZoomScale= 1将初始缩放设置为当前图像大小。我已经给出了maximumZoomScale = 10,它可以从图像大小和容器帧大小的实际比率中计算出来。

代码语言:javascript
复制
[scrollView addSubview: iImageView];
    scrollView.contentSize = iImageView.frame.size;
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 10;
    [iImageView setFrame:CGRectMake(0, 0, 450, 320)];
[scrollView scrollRectToVisible:iImageView.frame animated:YES];
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3869779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档