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

标签 iphone ios scale autorotate

我有一个自定义的 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/

相关文章:

ios - 支持信用卡的 Paypal MPL SDK

delphi - 调整图像大小会使鼠标 X/Y 变形

android - ExoPlayer 视频不裁剪和适合

html - 当页面太短时,div 中的背景图像在顶部和底部被截断

iPhone 5 屏幕尺寸 VS CSS 媒体查询

ios - iOS 不同设备的导航栏颜色略有不同

iphone - 在 viewWillAppear 中添加 AlertView,iPhone

ios - UICollectionView 的 cellForItemAtIndexPath 无法识别单个单元格框架

ios - 在 swift 4 中比较 Day from Date 的问题

javascript - CSS 动画仅在 iPad 上导致 Phonegap App 崩溃