2010-09-24 56 views
6

我正在開發iPad應用程序,並試圖處理多個方向。 我的應用程序包含一個webview和加載UIImageView,當我的webview加載內容時出現。iPad SDK,如何使用UIImageView處理方向

此UIImageView具有我在InterfaceBuilder中設置的背景圖像。當我將方向改爲橫向時,圖像被切割。

我想讓UIImageView在ipad處於縱向模式時設置image-portrait.png,當它處於橫向模式時設置image-landscape.png。

謝謝你的幫助和建議!

截圖:

alt text alt text

+0

究竟你「一刀切」的意思麼?一張照片會有幫助。 – colithium 2010-09-24 10:21:48

+0

縱向模式下我的圖像爲768x1004。在橫向模式下,它仍然是768px寬而不是1024px。我會發佈一個屏幕。 – Salah 2010-09-24 11:58:29

+0

下面是肖像截圖:http://img202.imageshack.us/img202/9617/screenshot20100924at141.png和風景:http://img832.imageshack.us/img832/9617/screenshot20100924at141.png – Salah 2010-09-24 12:15:51

回答

7

我找到了一個解決方案:

在Interface Builder中,我把我的ImageView的自動的自動調整大小填滿整個屏幕。 在我的ViewController,我添加了一個方法來檢測方向的變化,我設置了適當的圖像取決於如果iPad是在肖像或風景:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){ 
    myImageView.image = [UIImage imageNamed:@"image-landscape.png"]; 
} else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){ 
    myImageView.image = [UIImage imageNamed:@"image-portrait.png"]; 
} } 
+1

這是使用'UIInterfaceOrientationIsLandscape' &&'UIInterfaceOrientationIsPortrait' – 2013-04-14 21:40:52

5

我花了一段時間來理解這個概念。我不想創建相同的圖像肖像和風景。這裏的關鍵是CGAffineTransformMakeRotation從你的UIImageView或任何UIView的原始狀態旋轉。這假定你的背景圖像有定位。例如。你希望你的UIImageView保持放置,而其他對象的行爲正常的方向更改事件。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
           duration:(NSTimeInterval)duration { 

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {   
     backgroundImage.transform = CGAffineTransformMakeRotation(M_PI/2); 
    } 
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){ 
     backgroundImage.transform = CGAffineTransformMakeRotation(-M_PI/2); 
    } 
    else { 
     backgroundImage.transform = CGAffineTransformMakeRotation(0.0); 
    } 
} 
1

雖然薩拉赫你的答案看起來不錯給我,我相信你可以做兩方面的改進在這裏:

  1. 設置此功能中的背景圖片:

    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    

    持續時間( NSTimeInterval)持續時間

    如果您在didRotateFromInterfaceOrientation 內做了更改,您將在完成後更改背景圖像至 旋轉IPad,並且從兩個背景圖像 的轉換不會平滑:您將清楚地看到新的背景圖像彈出在旋轉結束時使用 。

  2. 提高設置myImageView.image值:

    _myImageView.image = UIInterfaceOrientationIsLandscape(interfaceOrientation)? [UIImage imageNamed:@「image-landscape.png」]:[UIImage imageNamed:@「image-portrait.png」];

3

您可以通過自動調整視圖處理方向。

UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]]; 

imageView.frame = self.view.bounds; 
imageView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight 
iimageView.contentMode = UIViewContentModeScaleAspectFill; 

[self.view addSubview:imageView]; 
[self.view sendSubviewToBack:imageView]; 

[imageView release]; 

這將解決您的問題。

0

我實際上增加了另一個分支到docchang的代碼,因爲當iPad旋轉到肖像向上自己它使用肖像右側圖像,看起來有點奇怪。 我說,

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    { 
    imageBackgroundView.transform = CGAffineTransformMakeRotation(M_PI/2); 
    } 
else 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
    imageBackgroundView.transform = CGAffineTransformMakeRotation(-M_PI/2); 
    } 
    else 
    if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
    imageBackgroundView.transform = CGAffineTransformMakeRotation(M_PI); 
    } 
    else 
     { 
     imageBackgroundView.transform = CGAffineTransformMakeRotation(0); 
     } 
0

有意思的是,程序員有這樣一個艱難的時間(在這種情況下,字面意思)逆向思考。

試試這個(我是如何解決這個問題的)。

  1. 創建一個方形圖像。
  2. 設置的UIImageView的制約,以填補在
  3. 將模式設置爲方面填充屏幕(前置,頂部,底部)(這將放大圖像,但保持其寬高比常數)

完成。

這裏的關鍵,當然,前提是你要創建一個方形的圖像(這是,正如我上面所說,這個盒子;-)