1

我一直在尋找這方面的很多東西,並且找不到任何東西來幫助我。旋轉UIViewController來抵消UIInterfaceOrientation的變化

我有一個UIViewController包含在另一個UIViewController。當父級UIViewController旋轉時,從Portrait到LandscapeLeft說,我想讓它看起來好像孩子沒有旋轉。也就是說。無論父母的方向如何,我都希望孩子擁有與天空一致的方向。如果它有一個在Portrait中直立的UIButton,我希望按鈕的右側在UIInterfaceOrientationLandscapeLeft中「向上」。

這可能嗎?目前,我正在做這樣的嚴重的東西:

-(void) rotate:(UIInterfaceOrientation)fromOrientation: toOr:(UIInterfaceOrientation)toOrientation 
{ 
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeRight)) 
     || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeLeft))) 
    { 

    } 
    if(((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
     || ((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortrait))) 
    { 

    } 
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeLeft)) 
     || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeRight))) 
    { 

    } 
    if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
     || ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortrait))) 
    { 

    } 
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
     || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationPortrait))) 
    { 

    } 
    if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationLandscapeRight)) 
     || ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationLandscapeLeft))) 
    { 

    } 
} 

這似乎是一個非常好的浪費代碼。此外,我計劃使用CGAffineTransform(如引用此處:http://www.crystalminds.nl/?p=1102),但我很困惑我是否應該改變視圖的尺寸以匹配旋轉後的視圖。

這裏最大的噩夢是你必須跟蹤一個全球「定向」變量。如果你不這樣做,錯覺就會消失,ViewController會旋轉到任何地方。

我真的可以使用一些幫助,謝謝!

回答

5

你可以做的最好的事情是根據你的界面方向改變你的子視圖框架的幀。你可以這樣做:

#pragma mark - 
#pragma mark InterfaceOrientationMethods 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){ 
     //self.view = portraitView; 
     [self changeTheViewToPortrait:YES andDuration:duration]; 

    } 
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){ 
     //self.view = landscapeView; 
     [self changeTheViewToPortrait:NO andDuration:duration]; 
    } 
} 

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{ 

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

    if(portrait){ 
     //change the view and subview frames for the portrait view 
    } 
    else{ 
     //change the view and subview frames for the landscape view 
    } 

    [UIView commitAnimations]; 
} 

希望這會有所幫助。

+0

感謝Madhup。 當你建議「改變肖像視圖的視圖和子視圖框架」 - 這是我會做一個CGAffineTransform?或者我應該有某種單獨的方式來旋轉UIView/UIViewController?我需要所有元素以相同的方式行事,只是有不同的方向。 另外,你評論的self.view = portraitView是什麼意思?我會從一開始就使用這些方法,但我不知道如何獲得側身UITableView或側向UIButton或類似的東西。 再次感謝! – 2010-03-22 19:43:54

+0

@Peter Hajas:我不執行CGAffine轉換,而是從xib獲取縱向和橫向視圖的幀位置,並相應地更改我的子視圖的框架。 – 2010-03-23 04:16:30

+0

我試圖實現這個方法,但是每當我旋轉模擬器設備時,沒有任何方法被觸發。你知道爲什麼嗎? – singingAtom 2011-05-24 12:34:09

0

我意識到的東西..讓我們說我們的項目有視圖控制器的多層(如如果添加其他視圖控制器子視圖您的視圖控制器)

willRotateToInterfaceOrientation:持續時間方法將不會被調用的第二層ViewController ...

所以我做的是,在我從我的最上層初始化我的第二層視圖控制器後,然後當最頂層調用willRotateToInterfaceOrientation:duration方法時,我將調用willRotateToInterfaceOrientation:duration用於第二層視圖控制器以及