Flex 拖/放至拖动代理的精确坐标

发布于 2024-10-19 06:07:12 字数 138 浏览 3 评论 0原文

我在 Flex(Web)应用程序中添加了自定义拖放功能,除了一个微小的问题外,一切都工作正常。被放置的图像稍微偏离实际的拖动代理。我希望拖放的图像位于与拖动代理相同的位置。我从 VBox 拖动到面板。我似乎无法在实际代理坐标和删除的坐标之间建立联系。非常感谢:)

I added custom dragging and dropping in a flex (web) application and it all works fine except for a teeny weeny problem. The image been dropped drops a bit off the actual drag proxy. I want the image dragged and dropped to be in the exact place as the drag proxy. I dragging from a VBox to a Panel. I just cant seem to make the connection between the actual proxy coordinates and the dropped coordinates. Thanx a lot :)

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

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

发布评论

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

评论(1

残月升风 2024-10-26 06:07:12

这是一个老问题,但我确信其他人正在寻找此信息。经过大量调试后,我已经确定了一种方法来完成这项工作(Flex 4 ... 3 可能类似)。
问题是用户可以在任何地方抓取原始对象来开始拖动。当您放置对象时,您需要指定它的去向。为了完美地放下它,你需要知道用户在哪里抓住了物体。我无法在传递到 Drop 事件处理程序的 DragEvent 中找到任何对“对象到鼠标”x,y 坐标的引用。然而,此信息是在鼠标按下处理程序中传递的原始 MouseEvent (localX, localY) 中提供的。您需要做的就是保留这些坐标(我将它们存储在被拖动的对象中作为鼠标偏移变量)。当放置事件发生时,只需从 DragEvents currentTarget mouseX 和 mouseY 中分别减去它们即可。不管出于什么原因,我还需要从两个坐标中减去 1 以获得完美的落差。
这是一段代码,希望可以使它更清楚。

// draggable  object mouse down handler
private function mouseDownHandler(e:MouseEvent):void {
  // MenuObject is one of my custom objects
  var tempMenuObject:MenuObject = e.currentTarget as MenuObject;

  // The MenuObject contains its own custom drag proxy
  var tempProxy:Graphic = tempMenuObject.dragProxy;

  // HERE !!! --- persist the "mouse to object" coords
  tempMenuObject.mouseOffsetX = e.localX;
  tempMenuObject.mouseOffsetY = e.localY;

  DragManager.doDrag(e.currentTarget as IUIComponent, null, e, tempProxy);
}

// droppable containers drop handler
private function dragDropHandler(e:DragEvent):void {

  // HERE !!! --- set the dragged objects x y coords (needed to subtract ONE from both coords to get it perfect)
  e.dragInitiator.x = (e.currentTarget.mouseX - MenuObject(e.dragInitiator).mouseOffsetX) - 1;
  e.dragInitiator.y = (e.currentTarget.mouseY - MenuObject(e.dragInitiator).mouseOffsetY) - 1;

  // add the item to the container
  e.currentTarget.addElement(e.dragInitiator);
}

希望它可以节省其他人的时间......

This is an old question but Im sure others are looking for this information. After a lot of debugging I have determined a way to make this work (Flex 4 ... 3 may be similar).
The issue is that the user can grab the original object anywhere to start the drag. When you drop the object you need to specify where it goes. To drop it perfectly you need to know where the user grabbed the object. I was unable to find any reference to the "object to mouse" x,y coords in the DragEvent passed into the Drop Event handler. However this information IS provided in the original MouseEvent (localX, localY) passed in the Mouse Down handler. All you need to do is persist these coords (I stored them in the object being dragged as mouse offset variables). When the drop event happens just subtract them respectively from the DragEvents currentTarget mouseX and mouseY. For whatever reason I also needed to subtract 1 from both coords to get the drop perfect.
Here is a chunk of code to hopefully make it clearer.

// draggable  object mouse down handler
private function mouseDownHandler(e:MouseEvent):void {
  // MenuObject is one of my custom objects
  var tempMenuObject:MenuObject = e.currentTarget as MenuObject;

  // The MenuObject contains its own custom drag proxy
  var tempProxy:Graphic = tempMenuObject.dragProxy;

  // HERE !!! --- persist the "mouse to object" coords
  tempMenuObject.mouseOffsetX = e.localX;
  tempMenuObject.mouseOffsetY = e.localY;

  DragManager.doDrag(e.currentTarget as IUIComponent, null, e, tempProxy);
}

// droppable containers drop handler
private function dragDropHandler(e:DragEvent):void {

  // HERE !!! --- set the dragged objects x y coords (needed to subtract ONE from both coords to get it perfect)
  e.dragInitiator.x = (e.currentTarget.mouseX - MenuObject(e.dragInitiator).mouseOffsetX) - 1;
  e.dragInitiator.y = (e.currentTarget.mouseY - MenuObject(e.dragInitiator).mouseOffsetY) - 1;

  // add the item to the container
  e.currentTarget.addElement(e.dragInitiator);
}

Hope it saves someone else some time ...

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