gpt4 book ai didi

iphone - 在自动旋转时重置 UIScrollView 的 zoomScale 属性

转载 作者:可可西里 更新时间:2023-11-01 06:18:19 25 4
gpt4 key购买 nike

我有一个自定义的 UIViewController,我在上面添加了一个 UIImageView(在 StoryBoard 上),然后将 UIImageView 嵌入到 UIScrollView 中。我的程序将图像加载到 imageView 中并将其缩放以适合。适当的配合,我的意思是:

  • 如果图像的纵横比与 View 的纵横比完全匹配,则图像将被缩放以完全适合 View ,没有滚动条
  • 否则,图像将被缩放以完全适合一个维度,并在另一个维度上使用滚动条。

我目前在 viewWillAppear 中执行此操作。下面是该方法的代码和计算正确 zoomScale 的辅助方法:

-(float) findZoomScale {    
float widthRatio = self.view.bounds.size.width / self.imageView.image.size.width;
float heightRatio = self.view.bounds.size.height / self.imageView.image.size.height;
float ratio;
if (widthRatio > heightRatio) ratio = widthRatio;
else ratio = heightRatio;
return ratio;
}

- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
self.imageView.image = self.image;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
self.scrollView.contentSize = self.imageView.image.size;
self.scrollView.zoomScale = [self findZoomScale];
[self.scrollView flashScrollIndicators];
}

- (void)viewDidLoad {
[super viewDidLoad];
self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.photoURL]];
self.imageView.image = self.image;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width,
self.imageView.image.size.height);
self.scrollView.delegate = self;
self.scrollView.zoomScale = 1.0;
self.navigationItem.title = self.photoTitle;
self.scrollView.autoresizesSubviews = YES;
self.scrollView.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
}

无论 iPhone 是横向还是纵向,此代码都按照我预期的方式工作,用于图像的初始加载。但是,当我随后在已加载图像的情况下旋转 iPhone 时,它​​无法正确缩放。我试图在 didRotateFromInterfaceOrientation 中进行重新缩放,使用 scrollView 的 zoomScale 属性,但我无法让它工作。这是进行重新缩放的正确位置吗?如果是这样,我该怎么做?谢谢。

杜安

最佳答案

我刚刚遇到了这个确切的问题(我假设你也在 CS193p 作业 4 上),并通过在调整 UIScrollView 的 contentSize< 之前将 zoomScale 设置回 1 解决了这个问题 旋转时。

例如:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//reset zoomScale back to 1 so that contentSize can be modified correctly
self.scrollView.zoomScale = 1;

// reset scrolling area equal to size of image
self.scrollView.contentSize = self.imageView.image.size;

//reset the image frame to the size of the image
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);

//set the zoomScale to what we actually want
self.scrollView.zoomScale = [self findZoomScale];
}

关于iphone - 在自动旋转时重置 UIScrollView 的 zoomScale 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9102488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com