Flex: Determine if a component is showing

后端 未结 6 1451
太阳男子
太阳男子 2021-02-20 02:42

What is the best way to determine if a component in Flex/Flash is showing on the user\'s screen? I\'m looking for an analog to Java\'s Component.isShowing() method.

The

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-20 03:02

    I was trying to obtain the same in a reusable manner.. I almost found out a way using getObjectsUnderPoint() - this returns the object under a particolar point, z-ordered (even if they are not siblings, e.g. ViewStack, Popups, ecc.).

    Basically, I get the topmost display object on under a particular point of the stage, then go up un the display object hierarchy to find the tested object. If I find it, the object is visible (not visible objects in the hierarchy should be already filtered out by the getObjectsUnderPoint call).

    The problem here is that you must use a non-transparent point of your object (in my case, I used an offset of 5 pixel due to rounder borders), otherwise it will not be picked up by this function.

    Any ideas to improve it?

    Cosma

    public static function isVisible(object:DisplayObject):Boolean  {
        var point:Point = object.localToGlobal(new Point(5, 5));
        var objects:Array = object.stage.getObjectsUnderPoint(point);
        if (objects.length > 0) {
            if (isDescendantOf(object, objects[objects.length - 1] as DisplayObject)) {
                return true;
            }
        }
        return false;
    }
    
    public static function isDescendantOf(parent:DisplayObject, child:DisplayObject):Boolean  {
        while (child.parent != null) {
            if (child.parent === parent) {
                return true;
            } else {
                child = child.parent;
            }
        }
        return false;
    }
    

提交回复
热议问题