AIR for iOS で Objective-C の shouldAutorotateToInterfaceOrientation
をどう検知する。
簡単なスクリプトで試してみました。
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" applicationComplete="didLaunch();"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.core.UIComponent; private var _bg:Shape; private var _circle:Shape; private function didLaunch():void { addEventListener(Event.RESIZE, onResize); this.stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, onResize); this.stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, onResize); } private function onResize(e:Event):void { trace(e, stage.stageWidth, stage.stageHeight); } ]]> </fx:Script> </s:Application> |
イベントの通知順序は以下のとおり
[Event type="resize" bubbles=false cancelable=false eventPhase=2] 640 960 [StageOrientationEvent type="orientationChanging" bubbles=false cancelable=true beforeOrientation="default" afterOrientation="rotatedRight"] 640 960 [StageOrientationEvent type="orientationChange" bubbles=false cancelable=false beforeOrientation="default" afterOrientation="rotatedRight"] 960 640 |
Event.RESIZE
StageOrientationEvent.ORIENTATION_CHANGING
- StageOrientationEvent.ORIENTATION_CHANGE
StageOrientationEvent.ORIENTATION_CHANGE
が最後に通知されました。
向き変更後のステージ幅・高さ(stage.stageWidth, stage.stageHeight
)は最後の StageOrientationEvent.ORIENTATION_CHANGE
でないと取得することができませんでした。
StageOrientationEvent.ORIENTATION_CHANGING
は今まさに向きを変えようとしているときのイベントなので理解できます。
Event.RESIZE
は StageOrientationEvent.ORIENTATION_CHANGING
よりも先に通知されるので、まだ向きを変えた状態にはなってないということなんでしょう。
向きの変更検知はStageOrientationEvent.ORIENTATION_CHANGE
向きの検知後に再配置スクリプトを書きたいときは StageOrientationEvent.ORIENTATION_CHANGE
を使うのが吉なようです。
StageOrientationEvent
は stage
に addEventListener
しないといけません。
名称に “Stage” って入っているからなんでしょう。
iPhone 4 simulator で試してます。
-app.xmlはこんな感じ。
<initialWindow> <autoOrients>true</autoOrients> <fullScreen>true</fullScreen> <visible>true</visible> <softKeyboardBehavior>none</softKeyboardBehavior> </initialWindow> <iPhone> <InfoAdditions><![CDATA[ <key>UIDeviceFamily</key> <array> <string>1</string> <string>2</string> </array> ]]></InfoAdditions> <requestedDisplayResolution>high</requestedDisplayResolution> </iPhone> |