Flex: Determine if a component is showing

后端 未结 6 1782
太阳男子
太阳男子 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 02:54

    UIComponent.visible is not necessarily valid for children of an object where visible=false. From the docs:

    "In either case the children of the object will not emit a show or hide event unless the object has specifically written an implementation to do so."

    I wrote a sample application that confirms this to be true. What you can do is walk up the display list checking for visible to be false on a parent. Basically "visible" gives false positives but shouldn't give false negatives. Here is a quick utility I put together:

    package
    {
        import flash.display.DisplayObject;
    
        import mx.core.Application;
    
        public class VisibilityUtils
        {
            public static function isDisplayObjectVisible(obj : DisplayObject) : Boolean {
                if (!obj.visible) return false;
                return checkDisplayObjectVisible(obj);
            }
    
            private static function checkDisplayObjectVisible(obj : DisplayObject) : Boolean {
                if (!obj.parent.visible) return false;
                if (obj.parent != null && !(obj.parent is Application))
                    return checkDisplayObjectVisible(obj.parent);
                else
                    return true;
            }
        }
    }
    

    I haven't done anything more than trivial tests on this but it should get you started.

提交回复
热议问题