【问题标题】:Calculating minimumZoomScale of a UIScrollView计算 UIScrollView 的 minimumZoomScale
【发布时间】:2011-04-21 15:38:01
【问题描述】:

我有一个图像,我想将其加载到图像视图中,并将 minimumZoomScale 以及 zoomScale 设置为我计算如下的 aspectFill 之类的比例:

// 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 320 像素,iImageSize 为 1600 x 1960 像素。

手动计算 minimumZoomScale:450 / 1960 = 0.22959184。 系统确定 0.234693885 代替 (???)。

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

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

关于这种不当行为的任何线索?

【问题讨论】:

  • 我也遇到过。我将 minimumZoomScale 设置为某个值,然后它会在某个点自动调整。调整或多或少不明显,我只是把它留在里面,然后添加了这一行 scrollview.zoomScale = scrollview.minimumZoomScale;只是为了确保一切都同步。

标签: iphone objective-c xcode


【解决方案1】:

不确定你是否还有这个问题,但对我来说,规模正好相反。 minimumZoomScale = 1 对我来说,我必须计算 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;

【讨论】:

    【解决方案2】:

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

    [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];
    

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多