如何限制画布中的拖放区域
我有一个画布,假设尺寸为 500x600。我在该画布内有一些控件。用户可以通过拖放重新排列控件。但我想限制该画布内的拖放。
例如:画布中有一个按钮。用户可以将按钮拖放到画布内的任何位置。但是,如果用户尝试将按钮拖出画布边界,则它应该停留在画布边界内。
如何实现这一目标?
I have a canvas,lets say of dimensions 500x600.I have some controls inside that canvas.User can rearrange the controls by drag and drop.But I want to restrict the drag and drop within that canvas.
For example:There is a button in the canvas.User can drag and drop the button anywhere inside the canvas.But if the user tries to drag the button out of the canvas boundaries,it should stick in the canvas boundary.
How to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
startDrag()
的签名是public function startDrag(lockCenter:Boolean = false,bounds:Rectangle = null):void
第二个参数允许您传递一个
矩形
作为您的DisplayObject
的边界。它不会被拖到这个之外The signature for
startDrag()
ispublic function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void
The second parameter allows you to pass a
Rectangle
to act as bounds for yourDisplayObject
. It won't be dragged outside of this您应该捕获目标控件上的 MouseDown 事件,然后订阅 MouseMove 事件。在 MouseMove 处理程序中,您应该获取 Canvas 和控件的矩形(在同一坐标空间中)并使用
containsRect()
方法(文档为 此处) 确定控件是否仍在 Canvas 内。如果它熄灭了,你就不应该移动它。并且不要记得取消订阅 MouseUp 上的 MouseMove 事件!
You should catch MouseDown event on the target controls and then subscribe MouseMove event. In MouseMove handler you should get Canvas and control's rectangles (in the same coordinate space) and use
containsRect()
method (documentation is here) to determine if control is still within Canvas. If it goes out you shouldn't move it.And don't remember unsubscribe your MouseMove event on MouseUp!