2012-02-29 76 views
0

我遇到模態視圖顯示的問題。我的第一個觀點旋轉得很好。模態視圖的控制器實現了「ShouldAutorotationToInterfaceRotation」方法,但是當我旋轉設備或模擬器時,模態視圖不會旋轉。我的模態視圖不旋轉

我已經添加了一些註釋來檢查第一個視圖是否檢測到旋轉事件而不是模態視圖,但不是,它不會獲取事件。

你有線索嗎?

回答

1

您將需要訂閱NSNotificationCenter才能接收您的模態視圖的旋轉事件。然後,如果要保持相對於底部視圖正確定向的模式視圖,則一種解決方案是轉換視圖。這裏有一個實現:

將這個在viewDidLoad中和原來的方向保存爲一個角度:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) 
              name:UIDeviceOrientationDidChangeNotification object:nil]; 

int initialOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
if (initialOrientation == UIDeviceOrientationPortrait) 
    initialAngle = 0.0; 
else if (initialOrientation == UIDeviceOrientationLandscapeRight) 
    initialAngle = 90.0; 
else if (initialOrientation == UIDeviceOrientationPortraitUpsideDown) 
    initialAngle = 180.0; 
else if (initialOrientation == UIDeviceOrientationLandscapeLeft) 
    initialAngle = 270.0; 

currentAngle = initialAngle; 

然後定義功能orientationChanged:(NSNotification *)通知與代碼正確的相對旋轉到初始方向:

int newOrientation = [[notification object] orientation]; 
if (newOrientation == UIDeviceOrientationPortrait) 
    desiredAngle = 0.0; 
else if (newOrientation == UIDeviceOrientationLandscapeRight) 
    desiredAngle = 90.0; 
else if (newOrientation == UIDeviceOrientationPortraitUpsideDown) 
    desiredAngle = 180.0; 
else if (newOrientation == UIDeviceOrientationLandscapeLeft) 
    desiredAngle = 270.0; 

if(desiredAngle != currentAngle) 
{ 
    CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI * (-desiredAngle+initialAngle)/180.0);  
    CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0); 
    [[self releaseView] setTransform:CGAffineTransformConcat(translate, rotate)]; 
} 
currentAngle = desiredAngle;