研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
屏蔽拖动策略
块拖动策略是一个对象,用于确定块应如何处理拖动。它们会实现相应的逻辑,将代码块设为可拖动。通过创建新的块拖动策略,您可以切换块处理拖动的方式,而无需创建自定义可选择或处理选择。
例如,您可能希望在拖动时复制图块,而不是正常拖动。通过阻止拖动策略,您可以做到这一点。
除了 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):2024-04-12。
[null,null,["最后更新时间 (UTC):2024-04-12。"],[[["Block drag strategies dictate how blocks respond to drag events, offering an alternative to custom selectables."],["By implementing the `IDragStrategy` interface, developers can create custom drag behaviors like block duplication on drag."],["Drag strategies encompass the functionality of `IDraggable`, excluding the `getRelativeToSurfaceXY` method, which is handled internally by the block."],["Applying a custom drag strategy requires using `setDragStrategy` within the block's `init` method to associate the strategy with each block instance."]]],["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"]]