可拖动的CALayer [英] Draggable CALayer
问题描述
有什么方法可以使用户拖动CALayer?如果是,怎么办?
Any way of making a CALayer draggable by the user? If so, how?
(在Cocoa-Mac中)
(In Cocoa - Mac)
推荐答案
图层本身无法接收鼠标事件。您必须在包含图层的视图或视图控制器中进行事件处理。
Layers can not receive mouse events themselves. You would have to do the event handling in the view or view controller containing the layer.
如果 mouseDragged:
事件起源于一层(请参见-[CALayer hitTest:]
和-[CALayer containsPoint:]
进行测试) ,相应地调整图层的位置
。您可能要禁用隐式动画,以使图层立即跟随鼠标指针(而不是由于position属性的动画而滞后一点):
If a mouseDragged:
event originates on a layer (see -[CALayer hitTest:]
and -[CALayer containsPoint:]
to test this), adjust the layer's position
accordingly. You will probably want to disable implicit animations to have the layer follow the mouse pointer immediately (rather than lagging a little behind because of the animation of the position property):
[CATransaction begin];
[CATransaction setDisableActions:YES];
layer.position = ...;
[CATransaction commit];
这篇关于可拖动的CALayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!