以编程方式更改 Scroller 容器的位置(通过 HGroup 索引)

发布于 2025-01-04 02:41:55 字数 886 浏览 6 评论 0原文

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

容器代码:

<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>

因此,例如,如果“BrigadeEmblem1”在滚动条中可见,我想在听到特定事件时以编程方式将可见图像更改为“BrigadeEmblem4”。

I am trying to change the visible index of a Scroller container on an event. The Scroller contains an HGroup with Images, so on an event I want to change which of the Images is visible in the Scroller by specifying an HGroup index. Is this possible?

Container code:

<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>

So, for example, if "BrigadeEmblem1" is visible in the Scroller, I want to programmatically change the visible image to "BrigadeEmblem4" if a specific event is heard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霓裳挽歌倾城醉 2025-01-11 02:41:55

正是如此!

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

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

Exactly That!

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

    function image1(){
    myMovieClip .visible = false;
    }
北城孤痞 2025-01-11 02:41:55

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

我通过扩展 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);

Yes, actually this is possible.

I've done it by extending the Scroller-class and creating this method:

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;
        }
    }

So when you want the second image:

myScroller.ensureIndexIsVisible(1);
平安喜乐 2025-01-11 02:41:55

答案有点晚了,但我希望这对某人有用。我编写了一些自动滚动 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;
 }

It is a little bit late for the answer but I hope this could be useful for someone. I have written some code that automatically scrolls the Scroller so the focused component is completely visible. You can use it to solve your problem

 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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文