Blockly 将于 2025 年 11 月 10 日迁移到 Raspberry Pi Foundation!阅读博客和常见问题解答。
自定义块拖动策略
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
图块拖动策略是一种对象,用于确定应如何处理图块
拖动。它们实现了相应逻辑,使块变为可拖动。
通过创建新的块拖动策略,您可以切换块应如何处理拖动操作,而无需处理创建自定义可选择项或处理选择操作。
例如,您可能希望在拖动时复制砌块,而不是
以便正常拖动。块拖动策略可以帮助您做到这一点。
除了 getRelativeToSurfaceXY 方法外,拖动策略与 IDraggable 具有相同的所有方法。
实现
如需创建拖动策略,您需要实现 IDragStrategy 接口。这需要所有相同的方法
用作 IDraggable 接口(getRelativeToSurfaceXY 方法除外),
已实施的屏蔽规则
您可以按照实现可拖动项的说明实现拖动策略,但请跳过实现 getRelativeToSurfaceXY()。
用法
如需使用自定义拖动策略,您需要对每个对象应用拖动策略
块的实例。您可以在代码块的 init 方法中通过调用 setDragStrategy 来实现此目的。
Blockly.Blocks['my_block'] = {
init: function() {
// Other initialization...
this.setDragStrategy(new MyDragStrategy());
// Other initialization...
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[],["Block drag strategies dictate how a block behaves during drags, allowing for custom drag behaviors like duplication. Developers implement the `IDragStrategy` interface, which mirrors `IDraggable` methods except for `getRelativeToSurfaceXY`. To apply a custom strategy, use the `setDragStrategy` method within a block's `init` method. This enables switching block drag behavior without altering core selection or drag handling. You must implement all the methods of `IDraggable` in the custom strategy except for `getRelativeToSurfaceXY`.\n"]]