缩放图像以适应 iPhone 旋转时的屏幕

Scale image to fit screen on iPhone rotation(缩放图像以适应 iPhone 旋转时的屏幕)

本文介绍了缩放图像以适应 iPhone 旋转时的屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 UIImageView 的 UIScrollView,但当 iPhone 旋转时我很难获得正确的行为.

I have a UIScrollView containing an UIImageView and I have some trouble to get the correct behavior when the iPhone rotates.

目标:从纵向到横向时,我试图获得以下内容:

Goal: I am trying to obtain the following when going from portrait to landscape:

_________
|AAAAAAA|
|BBBBBBB|         _________________
|CCCCCCC|         |     AAAAAA     |
|DDDDDDD|    -->  |     CCCCCC     |
|EEEEEEE|         |     EEEEEE     |
|FFFFFFF|         |_____GGGGGG_____|
|GGGGGGG|
---------

当 iPhone 旋转时,纵向视图中的整个图像被缩放以适应横向视图.它也是居中的.我也试图保持纵横比.用户交互也已开启,用户应该能够使用整个屏幕来平移/缩放图像.

Here the entire image in the portrait view is scaled to fit in the landscape view when the iPhone rotates. It is also centered. I am also trying to preserve the aspect ratio. User interaction is also on, and the user should be able to use the entire screen to pan/zoom the image.

目前我在滚动视图上有以下 autoresizingMask:

Currently I have the following autoresizingMask on the scrollView:

 scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth |
                               UIViewAutoresizingFlexibleHeight);       

但这给出了以下内容

_________
|AAAAAAA|
|BBBBBBB|         _________________
|CCCCCCC|         |AAAAAA          |
|DDDDDDD|    -->  [BBBBBB          |
|EEEEEEE|         [CCCCCC          |
|FFFFFFF|         [DDDDDD__________|
|GGGGGGG|
---------

此设置保留从左上角的比例和偏移.

This setting preserves scale and offset from upper left corner.

问题:是否可以使用合适的 autoresizingMask 获得这种行为?如果没有,可能应该设置

Question: Is it possible to obtain this behaviour using suitable autoresizingMask? If not, one should probably set

 scrollView.autoresizingMask = UIViewAutoresizingNone;

并在旋转时为 UIScrollView 手动设置 zoomScalecontentOffset.但是,应该在哪里做呢?为这种变化设置动画怎么样?

and manually set zoomScale and contentOffset for the UIScrollView on rotation. But, where should that be done? What about animating that change?

解决方案:通过稍微修改下面的答案,我得到了上述行为以下代码:

Solution: By very slightly modifying the answer below I got the above behaviour using the below code:

imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                     UIViewAutoresizingFlexibleHeight);

scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth 
                                     | UIViewAutoresizingFlexibleHeight);

imageView.contentMode = UIViewContentModeScaleAspectFit;
scrollView.contentMode = UIViewContentModeCenter;

推荐答案

试试这个:

scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                           UIViewAutoresizingFlexibleHeight);    
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                           UIViewAutoresizingFlexibleHeight);

imageView.contentMode = UIViewContentModeAspectFit; // You could also try UIViewContentModeCenter

我不确定 imageView 是否会在 UIScrollView 中自动调整大小,所以如果自动调整大小不起作用,您可以尝试自己设置框架旋转以等于滚动视图的大小.

I'm not sure if the imageView will get automatically resized within a UIScrollView, so if the autoresizing doesn't work, you could try setting the frame yourself on rotate to equal the size of the scrollview.

这篇关于缩放图像以适应 iPhone 旋转时的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:缩放图像以适应 iPhone 旋转时的屏幕

基础教程推荐