Flex: getting the height of a collection of controls

自古美人都是妖i 提交于 2019-12-11 16:26:02

问题


Or putting it more accurately, I want to be able to get the distance between the top of a control to the top of one of its children (and adding the height member of all the above children yields specious results!) but the process of getting the absolute coordinates, and comparing them, looks really messed up.

I use this function to calculate the height between the tops of 2 tags:

        private static function GetRemainingHeight(oParent:Container, oChild:Container,  
                            yParent:Number, yChild:Number):Number {
            const ptParent:Point = oParent.localToGlobal(new Point(0, yParent));
            const ptChild:Point = oChild.localToGlobal(new Point(0, yChild));
            const nHeightOfEverythingAbove:Number = ptChild.y - ptParent.y;
            trace(ptChild.y.toString() + '[' + yChild.toString() + '] - ' +
            ptParent.y.toString() + '[' + yParent.toString() + '] = ' + nHeightOfEverythingAbove.toString() + ' > ' + oParent.height.toString());
            return nHeightOfEverythingAbove;
         }

Note that oParent.y == yParent and oChild.y == yChild but I did it this way for binding reasons. The result I get is very surprising:

822[329] - 124[0] = 698 > 439

which is impossible, because the top of oChild does not disappear below oParent. The only figure I find unexpected is ptChild.y. All the other numbers look quite sane. So I'm assuming that my mistake was in subtracting two figures that are not supposed to be comparable.

Of course, if anyone has a method of calculating the difference between two points that doesn't involve localToGlobal(), that'd be fine, too.

I'm using the 3.5 SDK.


回答1:


I found a partial answer by looking to http://rjria.blogspot.ca/2008/05/localtoglobal-vs-contenttoglobal-in.html (including the comments). It dithers on whether or not I should be using localToGlobal() or contentToGlobal(), but it filled in some blanks that Adobe's documentation left, which is that you get the global coordinates by feeding the function new Point(0, 0). In the end, I used this:

    public static function GetRemainingHeight(oParent:DisplayObject, oChild:DisplayObject,
                yParent:Number, yChild:Number):Number {
        const ptParent:Point = oParent.localToGlobal(new Point(0, 0));
        const ptChild:Point = oChild.localToGlobal(new Point(0, 0));
        const nHeightOfEverythingAbove:Number = ptChild.y - ptParent.y;
        return nHeightOfEverythingAbove;
    }

See question for an explanation for the seemingly unnecessary parameters, which now seem like they might really be irrelevant.

However, I didn't need this function as often as I thought, and I'm not terribly happy w/the way it works anyway. I've learned that the way I've done it, it isn't possible to just make all those parameters to the function Bindable and expect this function to be called when changes to oChild are made. In one case I had to call this function in the handler for the updateComplete event.



来源:https://stackoverflow.com/questions/26150111/flex-getting-the-height-of-a-collection-of-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!