【问题标题】:flex: Drag and drop- object centeringflex:拖放对象居中
【发布时间】:2011-06-06 01:42:19
【问题描述】:

在使用 Flex 进行拖放的情况下,我试图让对象中心与放置点对齐 - 不知何故,不管对高度和宽度的调整如何,它总是将放置点定位到左上角。

这里是代码..

imageX = SkinnableContainer(event.currentTarget).mouseX;
imageY = SkinnableContainer(event.currentTarget).mouseY;

// Error checks if imageX/imageY dont satisfy certain conditions- move to a default position
// img.width and img.height are both defined and traced to be 10- idea to center image to drop point

Image(event.dragInitiator).x = imageX-(img.width)/2;
Image(event.dragInitiator).y = imageY-(img.height)/2

最后两行似乎没有任何效果。任何想法为什么 - 必须是直截了当的,我错过了......

【问题讨论】:

    标签: apache-flex drag-and-drop


    【解决方案1】:

    你可以使用下面的sn-p:

    private function on_drag_start(event:MouseEvent):void
    {
        var drag_source:DragSource = new DragSource();
        var drag_initiator:UIComponent = event.currentTarget as UIComponent;
    
        var thumbnail:Image = new Image();
        // Thumbnail initialization code goes here
    
        var offset:Point = this.localToGlobal(new Point(0, 0));
        offset.x -= event.stageX;
        offset.y -= event.stageY;
        DragManager.doDrag(drag_initiator, drag_source, event, thumbnail, offset.x + thumbnail.width / 2, offset.y + thumbnail.height / 2, 1.0);
    }
    

    这是一个重要的细节。 sn-p 使用舞台坐标系。 如果使用 event.localX 和 event.localY,这种方法在某些情况下会失败。例如,您单击并拖动影片剪辑。如果您使用 localX 和 localY 而不是舞台坐标,localX 和 localY 将定义影片剪辑当前单击部分的坐标,而不是整个影片剪辑。

    【讨论】:

      【解决方案2】:

      在 DragManager 的 doDrag 方法中使用 xOffsetyOffset 属性。

      here 为例。

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多