【问题标题】:Flex State transition弹性状态转换
【发布时间】:2010-06-19 03:39:58
【问题描述】:

我正在尝试创建状态之间的转换。第一个状态转换工作正常(state1=>state2),但第二个状态转换很奇怪(state2=>state3)。点击按钮更改后 state2=>state3,一些属于 state2 的文本区域会显示并停留在屏幕上。我不确定为什么。如果这里有人可以帮助我,我将不胜感激。

我的代码。

<?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" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
            protected function btn1_clickHandler(event:MouseEvent):void
            {
                currentState="state2";
            }

            protected function btn2_clickHandler(event:MouseEvent):void
            {
                currentState="state3";
            }



        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>





    <s:states>
        <s:State name="state1"/>
        <s:State name="state2"/>
        <s:State name="state3"/>
    </s:states>


    <s:transitions>

        <s:Transition toState="state2" >
            <s:Parallel>

            <s:Move targets="{[btn1,btn2]}" />
            <s:AddAction targets="{[label1,label2,label3,textInput1,textInput2,textInput3]}" /> 
            <s:Fade targets="{[label1,label2,label3,textInput1,textInput2,textInput3]}"/>

            </s:Parallel>

        </s:Transition>
        <s:Transition toState="state3" >
            <s:Fade targets="{[label1,label2,label3,textInput1,textInput2,textInput3]}"/>

        </s:Transition>

    </s:transitions>


    <s:Label y="10" text="label1" id="label1" includeIn="state2" />
    <s:TextInput y="30" id="textInput1" includeIn="state2" />
    <s:Label y="50" text="label1" id="label2" includeIn="state2" />
    <s:TextInput y="70" id="textInput2" includeIn="state2" />
    <s:Label y="90" text="label1" id="label3" includeIn="state2" />
    <s:TextInput y="120" id="textInput3" includeIn="state2" />



    <s:Button y="180" y.state2="350" label="btn1" id="btn1" click="btn1_clickHandler(event)"/>
    <s:Button y="250" y.state2="550" label="btn2" id="btn2" click="btn2_clickHandler(event)"/>



</s:Application>

【问题讨论】:

    标签: apache-flex animation state transition


    【解决方案1】:

    您的代码示例看起来不完整。我认为我们需要看到的不仅仅是状态和转换。如果您可以提供一个完整的运行示例,这将很有帮助。

    我认为这是一个示例问题,但在您当前的代码中,文档中没有创建到状态的转换。由于您有三个状态,您可能想要添加一个“fromState”并明确设计从/到每个状态的转换。

    如果我不得不猜测,您可能需要在相关组件上指定“includeIn”属性。您可以使用逗号分隔的状态列表,如下所示:

    <s:Component includeIn="a" />
    <s:Component includeIn="b,c" />
    <s:Component includeIn="a,c" />
    

    第一个组件出现在状态 a,第二个出现在状态 b 和 c,第三个组件出现在状态 a 和 c。


    更新海报代码:

        <?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" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    
        <fx:Script>
            <![CDATA[
                protected function btn1_clickHandler(event:MouseEvent):void
                {
                    currentState="state2";
                }
    
                protected function btn2_clickHandler(event:MouseEvent):void
                {
                    currentState="state3";
                }
    
    
    
            ]]>
        </fx:Script>
    
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
    
    
    
        <s:states>
            <s:State name="state1"/>
            <s:State name="state2"/>
            <s:State name="state3"/>
        </s:states>
    
    
        <s:transitions>
    
            <s:Transition toState="state2" >
                <s:Parallel>
    
                    <s:Move targets="{[btn1,btn2]}" />
                    <!--<s:AddAction targets="{[label1,label2,label3,textInput1,textInput2,textInput3]}" /> -->
                    <s:Fade targets="{[label1,label2,label3,textInput1,textInput2,textInput3]}" alphaFrom="0" alphaTo="1"/>
    
                </s:Parallel>
    
            </s:Transition>
            <s:Transition toState="state3" >
                <s:Parallel>
    <!--                <s:Move targets="{[btn1,btn2]}" />-->
                    <!--<s:RemoveAction targets="{[label1,label2,label3,textInput1,textInput2,textInput3]}" /> -->
                    <s:Fade targets="{[label1,label2,label3,textInput1,textInput2,textInput3]}" alphaFrom="1" alphaTo="0"/>
                </s:Parallel>
    
            </s:Transition>
    
        </s:transitions>
    
    
        <s:Label y="10" text="label1" id="label1" includeIn="state2" />
        <s:TextInput y="30" id="textInput1" includeIn="state2" />
        <s:Label y="50" text="label2" id="label2" includeIn="state2" />
        <s:TextInput y="70" id="textInput2" includeIn="state2" />
        <s:Label y="90" text="label3" id="label3" includeIn="state2" />
        <s:TextInput y="120" id="textInput3" includeIn="state2" />
    
    
    
        <s:Button y="180" y.state2="350" includeIn="state1,state2,state3" label="To State 2" id="btn1" click="btn1_clickHandler(event)"/>
        <s:Button y="250" y.state2="550" includeIn="state1,state2,state3" label="To State 3" id="btn2" click="btn2_clickHandler(event)"/>
    
    
    
    </s:Application>
    

    【讨论】:

    • 谢谢。我刚刚更新了我的代码。我的组件中确实有我的 includeIn。我什至卸载了我的 flex 并重新安装,因为我认为这是 flex 的故障......动画完全让我失望......但是 +1..first
    • 顺便说一句。所有这些奇怪的行为都是因为我添加了“" 如果我​​删除其中的 1 个,动画就可以正常工作,只是它们都同时播放...
    • 你能提供可运行的代码来演示这个问题吗?例如,您的状态定义为 a、b 和 c,但组件仅引用名为“addRecommend”的状态和转换。当我添加一个名为 addRecommend 的状态时,我收到一个关于名为 dataGrid 的未定义属性的错误。
    • 很抱歉给您带来了困惑。我刚刚创建了具有相同场景的全新代码。它更简单,代码更少......请参阅更新。再次感谢!!!
    • 我在答案中添加了更新的代码。我玩了一下。由于我无法解释的原因,事情发生得奇怪而不一致。我在淡入淡出上指定了 alphaFrom 和 alphaTo,并为两个按钮指定了 includeIn 状态,一切似乎都开始工作了。由于您正在执行并行效果,因此我不确定您为什么需要 AddAction 标记。如果您想要更多,则必须详细说明您的具体问题是什么。 “奇怪”很难调试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多