2012-07-08 58 views
0

我想要創建一個UIVIew,當我調用shouldAutorotateToInterfaceOrientation時,它將不會旋轉,其他子視圖將會旋轉。call shouldAutorotateToInterfaceOrientation但鎖定旋轉的視圖

我想保留shouldAutorotateToInterfaceOrientation支持,而不是使用通知。

感謝

+0

你能告訴我一些代碼如何防止uiview旋轉?你的意思是平分抽籤方法嗎? – Janub 2012-07-08 15:24:32

回答

3

一定要準確地定義你由具有視圖時設備旋轉「不轉」的意思。旋轉可能意味着幾件事情,具體取決於您引用的座標系。想一想更好的方法就是,您希望您的視圖適合每個設備方向。

只是提醒,shouldAutorotateTo ...是由系統發送到您的視圖控制器。你不要自己調用它。它不會導致旋轉。它讓系統詢問您的視圖控制器支持哪些方向。

你的VC應該爲它支持的所有方向回答YES。受支持的方向是視圖爲響應設備方向更改而更改佈局的方向,因此如果對給定方向發生任何佈局更改,則對shouldAutorotateTo的回答可能爲YES。

更改給定接口方向的子視圖佈局主要是您的責任。視圖有一個autoresizingMask,它是一個向量,用於描述相對於父級的大小和位置的一些選項,這通常是足夠的。完全控制方向變化佈局的方法是通過實現willAnimateRotationToInterfaceOrientation。

例如,這裏是一個相當寬鬆的shouldAutorotate,使所有,但一個方向......

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

而且這裏是你將如何控制如何子視圖上旋轉佈局...

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

    UIView *testView = [self.view viewWithTag:16]; 

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
     // change frames here to make the ui appear according to your spec 
     // including however you define "not rotating" for each view 
     self.subviewA.frame = ..... 
     self.subviewB.frame = .....     
    } else { 
     self.subviewA.frame = ..... 
     self.subviewB.frame = ..... 
    } 
} 
0

如果你想讓一個UIView不能隨着方向旋轉,簡單的解決方案之一就是像這樣將視圖添加到應用程序的頂部窗口。因爲窗口不隨設備方向旋轉。

[[[[UIApplication sharedApplication]windows]objectAtIndex:0]addSubview:customView];