11.14.创建State的自定义动作(action)

11.14.1. 问题
我想在进入state 时创建State 对象的自定义动作。
11.14.2. 解决办法
创建一个IOverride 接口的实现类,重写所有需要传递自定义行为的所有方法。
11.14.3. 讨论
要实现额外的state 动作,你需要创建自定义IOverride 对象,当进入state 时去执行你自己的条件逻辑。任何实现此接口的对象都能被添加到state 的重写数组中--任何组件都有下列方法:
apply(parent:UIComponent):void
该方法应用重写,执行重写(override)的自定义行为。将被改变的Parent 参数值会被存储在此方法中以便当离开state 执行撤销操作。

initialize():void
初始化重写

remove(parent:UIComponent):void
删除重写。在apply() 方法中记住的值将被恢复。当进入状态时自动调用此方法。不应直接对其进行调用

下面的例子中,实现IOverride 接口的类叫CustomOverride ,通过apply 语句应用自定义条件逻辑:
+展开
-ActionScript
package oreilly.cookbook{
import flash.display.DisplayObject;
import mx.core.UIComponent;
import mx.states.IOverride;
public class CustomOverride implements IOverride {
private var widthValue:Number;
private var _target:DisplayObject;
public function CustomOverride(target:DisplayObject = null)
{
_target = target;
}
public function get target():DisplayObject {
return _target;
}
public function set target(value:DisplayObject):void {
_target = value;
//empty
public function initialize():void {}
//here we make sure to store the value of the parent before
we change it
//so that we can use this value in the remove method
public function apply(parent:UIComponent):void {
widthValue = _target.width;
if(_target.width > 500) {
_target.width = 500;
}
}
//here we use the stored value
public function remove(parent:UIComponent):void {
_target.width = widthValue;
}
}
}

这里新的CustomOverride 类被用来改变vbox 的大小,这个类可以被扩展去使用多个目标和做更可能多的事情而不仅仅局限于改变目标宽度,这里只是为了做个简单的演示:
+展开
-XML
<mx:states>
<mx:State name="openState">
<cookbook:CustomOverride target="{box1}"/>
</mx:State>
</mx:states>

加支付宝好友偷能量挖...


评论(0)网络
阅读(125)喜欢(0)flash/flex/fcs/AIR