2011-05-24 146 views
16

當應用旋轉時,準確放置和移動視圖的「正確」方式是什麼?也就是說,當用戶界面從縱向旋轉到橫向(反之亦然)時,如何對視圖的位置,大小和重排進行細粒度控制?我認爲我的兩個選擇是:旋轉放置和移動視圖(UIView)

  1. 使用兩個superviews(縱向和橫向)。旋轉時:在它們之間切換。
  2. 使用一個超級視圖。旋轉時:更改每個子視圖的框架,邊界和中心屬性。

如果你有足夠鮮明的佈局和元素兩種觀點,那麼第一種方式可能是不夠好。如果你的兩個視圖基本上是針對不同方向的大小相同的東西,那麼第二種方法可能是僅使用一個視圖進行處理的更好方法。

我懷疑前者可以用IB來完成,後者應該以編程方式完成。

portrait to landscape

回答

9

若要在你的子視圖的位置和大小細粒度的控制,當iPhone旋轉,改變子視圖的框架中的UIViewController方法willAnimateRotationToInterfaceOrientation:duration:。這個方法在一個動畫塊中被調用,所以你在其中創建的子視圖框架的所有更改都是動畫的。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
     // Portrait frames 
     self.subviewA.frame = CGRectMake(x, y, width, height); 
     self.subviewB.frame = CGRectMake(x, y, width, height); 
     self.subviewC.frame = CGRectMake(x, y, width, height); 
    } else { 
     // Landscape frames 
     self.subviewA.frame = CGRectMake(x, y, width, height); 
     self.subviewB.frame = CGRectMake(x, y, width, height); 
     self.subviewC.frame = CGRectMake(x, y, width, height); 
    } 
} 
+0

就是這樣,謝謝。我試圖設置變形屬性,但表現出一種奇怪的方式。 – 2012-05-18 10:46:34

0

這裏有一個想法。我擔心動畫會變得有趣,但試試看。

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

    if (toOrientation == UIInterfaceOrientationLandscapeLeft || 
     toOrientation == UIInterfaceOrientationLandscapeRight) { 
     [UIView beginAnimations:nil context:NULL]; { 
      [UIView setAnimationDuration:1.0]; 
     } 
      [UIView setAnimationDelegate:self]; 
     [viewA setFrame:CGRectMake(?,?,?,?)]; 
     [viewB setFrame:CGRectMake(?,?,?,?)]; 
     [viewC setFrame:CGRectMake(?,?,?,?)]; 
     } [UIView commitAnimations];  
    } else if (toOrientation == UIInterfaceOrientationPortrait || 
      toOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     [UIView beginAnimations:nil context:NULL]; { 
      [UIView setAnimationDuration:1.0]; 
     } 
     [UIView setAnimationDelegate:self]; 
     [viewA setFrame:CGRectMake(?,?,?,?)]; 
     [viewB setFrame:CGRectMake(?,?,?,?)]; 
     [viewC setFrame:CGRectMake(?,?,?,?)]; 
     } [UIView commitAnimations];  
    } 
+0

我認爲你需要'setFrame'而不是'setBounds' – Lukman 2011-05-24 03:02:39

0

我有一個想法,這對我來說工作正常,我現在用一個小視圖在視圖上它自身旋轉的裝置發生變化時的方向。使用一個通知程序,然後在方法中更改方向。

#define DegreesToRadians(x) (M_PI * x/180.0) 

.......

[[NSNotificationCenter defaultCenter] addObserver:showIndicator 
             selector:@selector(setProperOreintation) 
              name:UIDeviceOrientationDidChangeNotification 
              object:nil]; 

.......

-(void)setProperOrientation { 

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 


if (orientation == UIDeviceOrientationPortraitUpsideDown) 
    self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(180)); 

else if (orientation == UIDeviceOrientationLandscapeLeft) 
    self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(90)); 

else if (orientation == UIDeviceOrientationLandscapeRight) 
    self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(-90)); 

    [UIView commitAnimations]; } 

,而不是旋轉u可以使用翻譯或規模以下

0

有爲此提供了很多解決方案。其中之一就是您以前閱讀過的控件的動畫。另一種選擇是準備2 UIView一個用於縱向模式,另一個用於橫向模式。並在開關方向 - 加載適當的UIView。這是我在我的應用程序中使用的方式。因爲有時景觀和肖像之間的差異是巨大的。

在簡單情況下,就足以爲子視圖設置正確的autoresizingMask。閱讀它。它的工作非常好。