0

我正在爲iPad製作視頻播放器,並且在正確旋轉動畫時遇到了一些麻煩。通常我通過設置自動旋轉蒙版來處理旋轉,但是由於我正在使用視頻播放器,我想保留高寬比,我不知道如何使用自動旋轉蒙版來實現。如何動畫MPMoviePlayerController的旋轉?

我沒有做動畫一個快速和骯髒的解決方案只是爲了得到正確的行爲:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.75); 
} 

它工作正常,但它不是漂亮。我目前正在準備應用程序進行演示,所以現在正確工作是不夠的,它必須是漂亮的。

我嘗試以下,我想你能猜到爲什麼它不工作:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [UIView animateWithDuration:duration animations:^{ 
     moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.75); 
    }]; 
} 

這是正確的,self.view.frame尚未更新。

有關如何處理這個,沒有硬編碼幀的任何意見?

回答

0

有可能做到這一點更清潔的方式,但我計算新的框架應利用當前的和新的方向是什麼:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    CGSize size = self.view.frame.size; 
    CGRect newRect; 
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
    newRect = CGRectMake(0, 0, MIN(size.width, size.height), MAX(size.width, size.height)); 
    } else { 
    newRect = CGRectMake(0, 0, MAX(size.width, size.height), MIN(size.width, size.height)); 
    } 

    [UIView animateWithDuration:duration animations:^{ 
    player.view.frame = newRect; 
    }]; 
} 
+0

我的電影播放器​​是在拆分視圖,從而使將無法工作。寬度在703和768之間變化,高度基於寬度(請參閱發佈的代碼)。 – 2011-04-11 14:16:55

+0

@Erik難道你仍然能夠使用這種技術的變種?你知道當前的方向是什麼,並且你知道新的方向,所以如果是90度旋轉,換掉當前的寬度和高度,否則保持原樣。 – 2011-04-11 14:23:42

+0

我基本上需要知道'self.view.frame'如何在它改變之前改變。 – 2011-04-11 15:29:11