检测 Spark 组件何时因状态更改而显示或隐藏

发布于 2024-11-15 19:16:59 字数 2550 浏览 2 评论 0原文

我有一个 Spark 组件,它在可见时运行一些动画(通过计时器)。

该组件应该:

  • 当计时器隐藏时暂停计时器,并
  • 在计时器再次显示时恢复计时器。

<!-- RandomButton.mxml-->
<?xml version="1.0"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          creationComplete="start()">
    <fx:Script><![CDATA[
        public var ticker:Timer = new Timer(1000, 0)

        private function start():void {
            ticker.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
                label = '' + Math.random()
            })
        }
    ]]></fx:Script>
</s:Button>

以下是如何使用 enterState + exitState 实现目标:

<!-- Main.mxml -->
<?xml version="1.0" ?>
<s:Application
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns="*">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>

    <s:states>
        <s:State name="STATE_1"
                 enterState="b.ticker.start()"
                 exitState="b.ticker.stop()"/>
        <s:State name="STATE_2"/>
    </s:states>

    <s:Button label="STATE_1" click="currentState='STATE_1'"/>
    <s:Button label="STATE_2" click="currentState='STATE_2'"/>

    <RandomButton id="b" includeIn="STATE_1"/>
    <s:Label text="Debug: {b.label}"/>

</s:Application>

单击 STATE_2 按钮时,计时器停止 - 您可以看到“调试”标签也停止。

您可以看到,RandomButton 需要一些外部“帮助”来处理其内部问题。这意味着将 EnterState/exitState 复制到要使用 RandomButton 的任何地方。这真是令人悲伤。

问题

我希望 RandomButton 自行维持其状态。像这样:

<!-- Main.mxml -->
...
    <s:states>
        <s:State name="STATE_1"/><!-- no need to poke inside the RandomButton -->
        <s:State name="STATE_2"/>
    </s:states>
...

<!-- RandomButton.mxml -->
<?xml version="1.0"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          creationComplete="start()"

          whenShown="ticker.start()"
          whenHidden="ticker.stop()" >
    ...
</s:Button>

属性 whenShownwhenHidden 不存在。这些只是一个愿望。

注意:有 showhide 属性,但它们处理 visible 属性(无论如何,它仍然设置为 true状态变化)。

I have a Spark component that runs some animations when is visible (through a Timer).

The component is supposed to:

  • suspend the timer when its is hidden and
  • resume the timer when its shown again.

.

<!-- RandomButton.mxml-->
<?xml version="1.0"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          creationComplete="start()">
    <fx:Script><![CDATA[
        public var ticker:Timer = new Timer(1000, 0)

        private function start():void {
            ticker.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
                label = '' + Math.random()
            })
        }
    ]]></fx:Script>
</s:Button>

Here is how the goal can be achieved with enterState + exitState:

<!-- Main.mxml -->
<?xml version="1.0" ?>
<s:Application
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns="*">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>

    <s:states>
        <s:State name="STATE_1"
                 enterState="b.ticker.start()"
                 exitState="b.ticker.stop()"/>
        <s:State name="STATE_2"/>
    </s:states>

    <s:Button label="STATE_1" click="currentState='STATE_1'"/>
    <s:Button label="STATE_2" click="currentState='STATE_2'"/>

    <RandomButton id="b" includeIn="STATE_1"/>
    <s:Label text="Debug: {b.label}"/>

</s:Application>

When STATE_2 button is clicked, the timer stops -- you can see that 'debug' label stops as well.

You can see that RandomButton needs some outside "assistance" to deal with its internal problems. This means copying enterState/exitState to any place where RandomButton is going to be used. Which is just sad.

THE QUESTION

I want RandomButton to maintain its state by itself. Like this:

<!-- Main.mxml -->
...
    <s:states>
        <s:State name="STATE_1"/><!-- no need to poke inside the RandomButton -->
        <s:State name="STATE_2"/>
    </s:states>
...

<!-- RandomButton.mxml -->
<?xml version="1.0"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          creationComplete="start()"

          whenShown="ticker.start()"
          whenHidden="ticker.stop()" >
    ...
</s:Button>

Attributes whenShown and whenHidden dont exist. These are just a wish.

NOTE: there are show and hide attributes, but they deal with visible property (which remains set to true regardless of state changes).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

智商已欠费 2024-11-22 19:16:59

这是解决方案:

<!-- RandomButton.mxml -->
<?xml version="1.0"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"

          addedToStage="ticker.start()"
          removedFromStage="ticker.stop()"
    ...
</s:Button>

Here is the solution:

<!-- RandomButton.mxml -->
<?xml version="1.0"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"

          addedToStage="ticker.start()"
          removedFromStage="ticker.stop()"
    ...
</s:Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文