缩放图像以适应iPhone旋转时的屏幕 [英] Scale image to fit screen on iPhone rotation
问题描述
我有一个包含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.
目前我有以下 scrollView上的autoresizingMask
:
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;
并手动设置 zoomScale
和<$ c旋转时UIScrollView的$ c> contentOffset 。但是,应该在哪里完成?那个变化的动画怎么样?
and manually set zoomScale
and contentOffset
for the UIScrollView on rotation. But, where should that be done? What about animating that change?
解决方案:
通过稍微修改下面的答案我使用$ b $得到了上述行为b以下代码:
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旋转时的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!