2013-03-08 62 views
0

我正在創建一個flex移動項目,我想強制應用程序以縱向方向當我點擊一個按鈕,當我點擊其他按鈕再次允許更改方向。Flex移動方向:強制肖像模式,然後允許自動方向

這是我的代碼,當我點擊第一個按鈕,在這裏我想強迫人像模式:

protected function click(event:MouseEvent):void{ 
    if(stage.orientation != StageOrientation.DEFAULT){ 
      stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged); 
      stage.setOrientation(StageOrientation.DEFAULT); 
    }else{ 
      doSomething(); 
    } 

} 


private function orientationChanged(event:StageOrientationEvent):void{ 
      doSomething(); 
      stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged); 
} 

private function doSomething():void{ 
      stage.autoOrients = false; 
} 

它工作正常,如果它需要它改變了方向。

現在,當我想再次允許方向變化,我只是推杆:

stage.autoOrients = true; 

如果當我點擊第一個按鈕的應用程序是在人像它工作正常,它不必改變一切。但是如果它在風景上,它必須更改爲肖像,當我再次允許方向更改時,它不起作用。

你知道我是否必須允許或改變某些東西嗎?或者,有沒有更好的方法來做到這一點?

在此先感謝

回答

0

這是一個小技巧,但我已經解決了它。

首先,如果方向必須改變,我保存舊的方向。當我點擊允許再次改變方向的按鈕時,首先我放置舊的方向,然後我允許自動方向。

這是新代碼的一部分:

private var oldOrientation:String = null; 

protected function click(event:MouseEvent):void{ 
    if(stage.orientation != StageOrientation.DEFAULT){ 
      oldOrientation = stage.orientation; 
      stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged); 
      stage.setOrientation(StageOrientation.DEFAULT); 
    }else{ 
      doSomething(); 
    } 

} 


private function orientationChanged(event:StageOrientationEvent):void{ 
      // do something if you are changing to portrait mode (the first change) and other thing if you are changing to old orientation 
} 

該按鈕可讓autoorientation:

if(oldOrientation != null){ 

     stage.setOrientation(oldOrientation); 
     oldOrientation = null;    
} 
+0

HI, 我怎麼能強迫我的應用程序在橫向模式下,從來沒有到常開/行爲重新調整到肖像模式? 注意:我的應用程序是用於Ipad的。 謝謝。 – 2014-01-09 08:42:10