以编程方式更改Scroll容器的位置(按组索引)

时间:2012-02-10 20:46:08

标签: flex actionscript flex4 flash-builder

我正在尝试更改事件上Scroller容器的可见索引。 Scroller包含一个带有Images的HGroup,因此在一个事件中我想通过指定HGroup索引来更改Scroller中哪些Images可见。这可能吗?

货柜代码:

<s:Scroller id="test" includeIn="startState" x="53" y="20" width="170"
            height="200" depth="2" scrollSnappingMode="leadingEdge">
    <s:HGroup gap="0" width="170" height="200">
        <s:Image id="BrigadeEmblem1" width="170" height="200" smooth="true"
                 smoothingQuality="high" source="assets/1st Stryker Brigade.png"
                 verticalAlign="middle"/>
        <s:Image id="BrigadeEmblem4" width="170" height="200" smooth="true"
                 smoothingQuality="high" source="assets/4th Stryker Brigade.png"
                 verticalAlign="middle"/>
    </s:HGroup>
</s:Scroller>

因此,例如,如果&#34; BrigadeEmblem1&#34;在Scroller中可见,我想以编程方式将可见图像更改为&#34; BrigadeEmblem4&#34;如果听到特定事件。

3 个答案:

答案 0 :(得分:0)

确切地说!

    setTimeout(image1,3000); // in 3 seconds this event will call for the function(image1)

    function image1(){
    myMovieClip .visible = false;
    }

答案 1 :(得分:0)

是的,实际上这是可能的。

我通过扩展Scroller类并创建了这个方法来完成它:

public function ensureIndexIsVisible(index:int):void {

        if (!viewport || !(viewport is GroupBase) || !(viewport as GroupBase).layout) {
            return;
        }

        var spDelta:Point = GroupBase(viewport).layout.getScrollPositionDeltaToElement(index);

        // if spDelta is null, no scrolling is required.
        if (spDelta) {
            GroupBase(viewport).horizontalScrollPosition += spDelta.x;
            GroupBase(viewport).verticalScrollPosition += spDelta.y;
        }
    }

所以当你想要第二张图片时:

myScroller.ensureIndexIsVisible(1);

答案 2 :(得分:0)

回答有点迟,但我希望这对某人有用。我编写了一些自动滚动Scroller的代码,以便聚焦的组件完全可见。您可以使用它来解决您的问题

 private var _focusedComponentPaddingTop:int = 10;
 private var _focusedComponentPaddingBottom:int = 10;
 private var _focusedComponentPaddingLeft:int = 5;
 private var _focusedComponentPaddingRight:int = 5;

 public function makeFocusedItemVisible(event:FocusEvent):void {
    // Target is the actual object that has focus.
    var target:DisplayObject = DisplayObject(event.target);

    if (target != null && contains(target)) {
       // The container's viewable area
       var visibleArea:Rectangle = getVisibleArea();
       var changed:Boolean = false;

       // Calculate the position of the target in the container.
       var topLeft:Point = new Point(0, 0);
       topLeft = target.localToGlobal(topLeft);
       topLeft = globalToLocal(topLeft);

       var bottomRight:Point =
             new Point(target.width, target.height);
       bottomRight = target.localToGlobal(bottomRight);
       bottomRight = globalToLocal(bottomRight);

       // Check if the component is visible and move the scrollbars if not
       if (bottomRight.x > visibleArea.right) {
          var deltaX:Number = bottomRight.x - visibleArea.right;
          viewport.horizontalScrollPosition += deltaX + _focusedComponentPaddingRight;
          topLeft.x -= deltaX;
          changed = true;
       }

       if (topLeft.x < visibleArea.left) {
          viewport.horizontalScrollPosition -=
                visibleArea.left - topLeft.x + _focusedComponentPaddingLeft;
          changed = true;
       }

       if (bottomRight.y > visibleArea.bottom) {
          var deltaY:Number = bottomRight.y - visibleArea.bottom;
          viewport.verticalScrollPosition += deltaY + focusedComponentPaddingBottom;
          topLeft.y -= deltaY;
          changed = true;
       }

       if (topLeft.y < visibleArea.top) {
          viewport.verticalScrollPosition -=
                visibleArea.top - topLeft.y + focusedComponentPaddingTop;
          changed = true;
       }

       // Call validateNow() to get the container move the component 
       if (changed) {
          validateNow();
       }
    }
 }

 private function getVisibleArea():Rectangle {
    var area:Rectangle = new Rectangle();

    area.x = x;
    area.y = y;
    area.width = width;
    area.height = height;

    return area;
 }