以上两种诡异特性
对于需要做对象缩放排版的功能时,很容易生成潜在的 Bug
尤其是需要开发或是更改 Flex UI Component 时
就算知道了,对于需要时间反映在对象尺寸特性
还是很难避免
难道每次更改过 scrollRect 之后,都要使用 setTimeout or callLater 吗?
以下分享一个小技巧
可以让你在变动过 scrollRect 之后,立即强迫 DisplayObject 更新尺寸相关属性
AS3 示范程序:
/*
Ticore's Blog
http://ticore.blogspot.com
*/
package {
import flash.display.*;
import flash.geom.*;
import flash.utils.*;
public class Main extends MovieClip {
var mc:MovieClip;
public function Main() {
doTest();
}
public function doTest() {
mc = new MovieClip();
this.addChild(mc);
var g:Graphics = mc.graphics;
g.beginFill(0xFF0000);
g.drawRect(0, 0, 100, 100);
g.endFill();
traceSize("After draw rectangle graphic", mc);
mc.scrollRect = new Rectangle(0, 0, 50, 50);
traceSize("After assign scrollRect", mc);
//*/
// Force DisplayObject update dimensions
var bmpData:BitmapData = new BitmapData(1, 1);
bmpData.draw(mc);
traceSize("After draw by BitmapData", mc);
//*/
setTimeout(traceSize, 0, "0 ms later", mc);
setTimeout(traceSize, 10, "10 ms later", mc);
setTimeout(traceSize, 500, "500 ms later", mc);
}
public function traceSize(msg:String, target:DisplayObject):void{
trace("----------------------------------------------------");
trace(msg, ", time:" + getTimer());
trace("width:", mc.width, ", height:", mc.height,
", bounds:", mc.getBounds(mc.parent));
}
}
}
输出结果:
----------------------------------------------------
After draw rectangle graphic , time:7
width: 100 , height: 100 , bounds: (x=0, y=0, w=100, h=100)
----------------------------------------------------
After assign scrollRect , time:13
width: 100 , height: 100 , bounds: (x=0, y=0, w=100, h=100)
----------------------------------------------------
After draw by BitmapData , time:45
width: 50 , height: 50 , bounds: (x=0, y=0, w=50, h=50)
----------------------------------------------------
0 ms later , time:85
width: 50 , height: 50 , bounds: (x=0, y=0, w=50, h=50)
----------------------------------------------------
10 ms later , time:92
width: 50 , height: 50 , bounds: (x=0, y=0, w=50, h=50)
----------------------------------------------------
500 ms later , time:571
width: 50 , height: 50 , bounds: (x=0, y=0, w=50, h=50)
相关连结:
AS - scrollRect 与 Mask 差异
What you should know about DisplayObject’s mask and scrollRect