問題描述
我有一個包含 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 旋轉時的屏幕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!