Scale image to fit screen on iPhone rotation(缩放图像以适应 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 手动设置 zoomScale
和 contentOffset
.但是,应该在哪里做呢?为这种变化设置动画怎么样?
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 旋转时的屏幕
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01