2011-06-01 39 views
0

我有一個項目,其中旋轉事件在刪除/添加大量視圖/ viewControllers後停止觸發。它工作了一段時間,然後隨機添加到窗口的新視圖永遠不會獲取shouldAutorotateToInterfaceOrientation:調用。shouldAutorotateToInterfaceOrientation:在一段時間後無法調用

狀態欄無法旋轉,但視圖/視圖控制器功能正常(減旋轉)。此外,一次只有一個view/viewController添加到窗口中。我假設一箇舊的viewController被註冊爲鍵,但是添加到窗口的所有東西都是視圖控制器的子類,並且除了活動分配以外,沒有任何子類顯示在儀器分配中。

有沒有在應用程序委託或窗口找到哪個視圖控制器是關鍵的方法?

爲什麼shouldAutorotateToInterfaceOrientation:可能無法被調用嗎?

每個視圖控制器實施:

- (BOOL)shouldAutorotateToInterfaceOrientation: 

(UIInterfaceOrientation)interfaceOrientation { 返回YES; }

- (void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) 
        interfaceOrientation duration: (NSTimeInterval) duration { 
    //manual rotation code 
} 

這裏是我使用在應用程序委託以改變根視圖控制器和所顯示的視圖中的代碼。

[UIView transitionWithView:new.view 
        duration:0.3 
        options:UIViewAnimationOptionCurveEaseOut 
       animations:^{ 
        //add new view 
        [self.window addSubview:new.view]; 

        //play transition animation for old and new views 
        new.view.transform = CGAffineTransformConcat(new.view.transform, newEnd); 
        old.view.transform = CGAffineTransformConcat(old.view.transform, oldEnd); 

        //if old view is a "modal style" then keep it in front on new view 
        if([old conformsToProtocol:@protocol(PreloadableModalView)]) { 
         [self.window bringSubviewToFront:old.view]; 
        } 

       } 
       completion:^(BOOL finished){ 
        //remove old view 
        [old removeSubviews]; 
        [old.view removeFromSuperview]; 
       }]; 
+0

ViewControllers不是「鍵」 - 一個單一的窗口是應用程序鍵。您不要將viewController添加到窗口 - 您添加視圖。你怎麼知道shouldAutoRotate沒有被調用? – Rayfleck 2011-06-01 14:07:19

+0

我正在傳遞視圖[窗口addSubview:new.view]不是該控制器,這將允許UIWindow使用UIView的nextResponder屬性來查找我的viewController。 [蘋果關於呈現視圖控制器的文檔](http://bit.ly/jrMjE2)。 shouldAutorotate沒有被調用,因爲我在該函數中添加了一個NSLog,並且它從不觸發。我還添加了一個autorotate事件的觀察者,它註銷了它,但它並沒有幫助我,因爲如果我嘗試運行我的手動旋轉函數,狀態欄不會旋轉[[蘋果的文檔](http:/ /bit.ly/iQTXgk) – puppybits 2011-06-01 17:27:27

+0

嗯。這裏涉及到UITabBar(你提到「根視圖控制器」)嗎?在過去,我不得不創建一個UITabBarController的自定義子類,並用下面的代碼覆蓋它的shouldAutorotateToInterfaceOrientation:{return self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];}這樣shouldAutoRotate實際上就會傳播到子視圖控制器。 – Rayfleck 2011-06-01 18:56:32

回答

0

它看起來像問題的根源是,取出的viewController,窗口將派遣在過渡期間旋轉事件引起的窗口有時不登記新的視圖 - 控制作爲新的根。

我只有一個UIView(帶有自己的UIViewController)添加到窗口中。在動畫期間,我正在移除根視圖控制器並在屏幕上添加一個新視圖。解決的辦法是在動畫之前切換視圖控制器。

  1. 我所創建的當前視圖的屏幕截圖

  2. 刪除舊的視圖控制器

  3. 設置新的視圖控制器作爲RootViewController的和視圖添加到窗口

  4. 跑過渡動畫

  5. oncomplete我刪除圖像

這是代碼的主要部分:

//create a image of old view and add to window 
UIGraphicsBeginImageContext(old.view.bounds.size); 
[[old.view layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageView *screenshot = [[UIImageView alloc] initWithImage:snapshot]; 
[self.window addSubview:screenshot]; 
[screenshot release]; 

//remove view and viewcontroller 
[old.view removeFromSuperview]; 

//set new viewcontroller as root 
if(self.window.rootViewController) 
    self.window.rootViewController = nil; 
self.window.rootViewController = new; 

//transition code goes here 


//animate 
[UIView transitionWithView:new.view 
        duration:0.5 
        options:UIViewAnimationOptionCurveEaseOut 
       animations:^{ 

        //add new view 
        [self.window addSubview:new.view]; 

        //play transition animation 
        if(isOldModalDisplay) { 
         screenshot.transform = CGAffineTransformConcat(new.view.transform, newEnd); 
         [self.window bringSubviewToFront:screenshot]; 
        } else { 
         new.view.transform = CGAffineTransformConcat(new.view.transform, newEnd); 
        } 
       } 
       completion:^(BOOL finished){ 
        //remove screenshot 
        [screenshot removeFromSuperview]; 
       }]; 
相關問題