正确preventing方向变化的Flex移动应用程序 [英] Properly preventing orientation change in Flex Mobile app

查看:238
本文介绍了正确preventing方向变化的Flex移动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人能够真正使之在的Flex SDK 4.6正常工作?

Is anyone able to actually make it work properly in Flex SDK 4.6?

下面是一个简短的片断:

Here's a short snippet :

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        addedToStage="onAddedToStage(event)"
        title="Title">
    <fx:Script>
        <![CDATA[  
            private function onAddedToStage(event:Event):void { 
                if (stage.autoOrients) {
                    stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging, false, 0, true);
                }
            }

            private function orientationChanging(event:StageOrientationEvent):void {
                if (event.afterOrientation == StageOrientation.DEFAULT || event.afterOrientation == StageOrientation.UPSIDE_DOWN) {
                    event.preventDefault(); 
                }     
            } 
        ]]>
    </fx:Script>
</s:View>

我试图做到的,是支持横向模式在两个方向,因此,如果用户打开设备180辈分,屏幕也应该转动。但是,不应该有任何的动作可言,当用户旋转设备,以肖像方向之一。相反,我看到的宽度变更为导航操作栏,有时在纵向方向的内容,因此显然是preventing事件是不够的。我使用了官方的方式Adobe建议,但问题是,它不工作得非常好。诚然,在现阶段不会改变,但似乎有什么东西在燃烧导航无论如何,因为你可以看到操作栏的宽度变化。

What I'm trying to achieve is to support Landscape mode in both orientations, so if user turns the device 180 degress, the screen should also rotate. But there should be no action at all, when user rotates the device to one of portrait orientations. Instead, I'm seeing width changes to navigator action bar and sometimes content in portrait orientations, so apparently preventing the event is not enough. I'm using the "official" way Adobe suggests, but the problem is that it's not working very well. Granted, the stage does not change, but it seems that there's something firing in navigator anyway, since you can see the action bar width changing.

我有明确设置layoutBounds有固定宽度的处理方法取得了一些成功 - 这prevents改变动作条的宽度,但它只是一个临时的解决方案 - 如果认为是受一个过渡,或者一些其他的重绘 - 它会再次渲染坏的大小。因为如果有下面的东西告诉它,它是在肖像模式下,尽管我试图prevent吧。

I had some success with explicitly setting layoutbounds to fixed width in handler method - this prevents changing the actionbar width, but it's only a temporary solution - if the view is a subject to a transition, or some other redraw - it will again render with bad sizes. As if there was something below that was telling it that it's in portrait mode, even though I'm trying to prevent it.

在你引爆了一些愚蠢的想法,如autoOrient =假,没有。它显然不是针对此问题的解决方案。显然,这是用Flex SDK中的错误 - 有没有人找到一种方法来解决它还是一个稳定的解决方法吗?

Before you detonate with some silly ideas like "autoOrient = false", don't. It's clearly not a solution for this problem. Obviously it's a bug with Flex SDK - did anyone find a way to fix it or a stable workaround?

编辑:显然是其他人碰到类似的问题:
- http://forums.adobe.com/message/3969531 (主要议题是别的事情,但看过魔术机器人的评论)
- http://forums.adobe.com/message/4130972

apparently others bumped into similar issue:
- http://forums.adobe.com/message/3969531 (the main topic is about something else, but read magic robots's comment)
- http://forums.adobe.com/message/4130972

推荐答案

我不知道这是否是正确的,我是不是做错了什么事到了最后,但很多的斗争后,我发现这一个稳定的解决方案:

I'm not sure if this is the right one, did I do something wrong in the end, but after a lot of struggle, I've found this one to be stable solution:

private function onAddedToStage(event:Event):void { 
    if (stage.autoOrients) {
        stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging);
        stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging, false, 100, true);
    }
}      

private function orientationChanging(event:StageOrientationEvent):void {
    event.stopImmediatePropagation();
    if (event.afterOrientation == StageOrientation.DEFAULT || event.afterOrientation == StageOrientation.UPSIDE_DOWN) {
        event.preventDefault(); 
    }   
}  

首先要注意的是,addedToStage火灾移动应用几次(2-3)。我不知道为什么,有一个在我的code无的addChild,效果显着。也许AIR运行时做别的事情。因此,为了避免增加处理器不必要的数量,常用的方法是先删除处理程序 - 它不会做任何事情,如果这样的处理程序尚未注册,但如果有,它会删除它,这将保持处理器计数1.

First thing to note is that addedToStage fires few times (2-3) in mobile application. I don't know why, there's no addChild in my code, obviously. Maybe the AIR runtime does something else. So, to avoid adding unnecessary amount of handlers, the common technique is to remove handler first - it won't do anything, if such handler is not yet registered, but if there is, it will remove it, which will maintain the handler count on 1.

第二件事是事件的优先级 - 它不会在0正常工作,它必须对一些大集,之前的东西,在AIR运行时推出

Second thing is the priority of the event - it won't work on 0, it has to be set on something big, to launch before stuff in AIR runtime.

最后一件事 - event.stopImmediatePropagation() - 现在,我们是第一个来处理该事件,我们不能prevent本次活动得到进一步发在此特定情形。

Last thing - event.stopImmediatePropagation() - now, that we're the first to handle the event, we cant prevent this event to be sent further up in this particular scenario.

这一起使得定向preventing工作完美 - 我的景观和颠倒景观(ROTATED_LEFT,rotated_right)的工作和过渡,而纵向模式并没有影响到观点的。

This together makes the orientation preventing working perfectly - for me the landscape and the reversed landscape (rotated_left, rotated_right) were working and transitioning, while portrait modes did not affect the view at all.

现在,有危险在这里 - 你可能想在离开以删除侦听器(在过渡动画结束后,查看停用或东西),因为stopImmediatePropagation将prevent事件在应用程序的其他部分进行处理

Now, there's danger here - you might want to remove the listener upon leaving the view (on transition animation end, view deactivate or something), because stopImmediatePropagation will prevent the event to be handled in other parts of your application.

我希望的Adobe(或Apache现在,实际上)将采取仔细看看这个问题,并跟踪我的解决办法。

I hope Adobe (or Apache now, actually) will take a closer look at this problem and trace my solution.

修改

有remaisn与此解决方案,这是最后一个问题。

There remaisn a last issue with this solution, which is if application starts while device is in DEFAULT or UPSIDE_DOWN orientation, in this case application will be in portrait mode.

要解决这个问题,一个解决方案是在addedToStage处理程序改变长宽比:

To fix this, a solution is to change Aspect Ratio within addedToStage handler:

   if(Stage.supportsOrientationChange) {
         stage.setAspectRatio(StageAspectRatio.LANDSCAPE);
    }

这篇关于正确preventing方向变化的Flex移动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆