可拖动的CALayer

发布于 2024-10-19 06:14:14 字数 63 浏览 1 评论 0原文

有什么方法可以让用户拖动 CALayer 吗?如果是这样,怎么办?

(在可可 - Mac 中)

Any way of making a CALayer draggable by the user? If so, how?

(In Cocoa - Mac)

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

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

发布评论

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

评论(2

早茶月光 2024-10-26 06:14:14

图层本身无法接收鼠标事件。您必须在包含该层的视图或视图控制器中进行事件处理。

如果 mouseDragged: 事件源自图层(请参阅 -[CALayer hitTest:]-[CALayer containsPoint:] 来测试这一点),相应地调整图层的位置。您可能希望禁用隐式动画以使图层立即跟随鼠标指针(而不是因为位置属性的动画而稍微落后):

[CATransaction begin];
[CATransaction setDisableActions:YES];
layer.position = ...;
[CATransaction commit];

Layers can not receive mouse events themselves. You would have to do the event handling in the view or view controller containing the layer.

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];
ι不睡觉的鱼゛ 2024-10-26 06:14:14

我尝试通过代码创建一个窗口并向其中添加 CALayer,但我不明白为什么它没有显示。

NSRect rect = NSZeroRect;
    rect.size = NSMakeSize( SSRandomFloatBetween( 300.0, 200.0 ), SSRandomFloatBetween( 300.0, 200.0 ));

    NSWindow *newWin = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSWindowBackingLocationDefault defer:YES];
    [newWin setBackgroundColor: [NSColor clearColor]];
    [newWin setOpaque:NO];
    [newWin setIgnoresMouseEvents:NO];
    [newWin setMovableByWindowBackground:YES];
    [newWin makeKeyAndOrderFront:self];

    [[newWin contentView] setWantsLayer:YES];

    NSRect contentFrame = [[newWin contentView] frame];
    CALayer *newWinLayer = [CALayer layer];
    newWinLayer.frame = NSRectToCGRect(contentFrame);

    layer.backgroundColor=CGColorCreateGenericGray(0.0f, 0.5f);
    layer.borderColor=CGColorCreateGenericGray(0.756f, 0.5f);
    layer.borderWidth=5.0;

        // Calculate random origin point
    rect.origin = SSRandomPointForSizeWithinRect( rect.size, [window frame] );

        // Set the layer frame to our random rectangle.
    layer.frame = NSRectToCGRect(rect);
    layer.cornerRadius = 25.0f;
  [newWinLayer addSublayer:layer];

窗口链接到一个大窗口,其中有一个半透明(黑色填充)窗口,可调整大小以填充屏幕。

我已经将窗口设置为可拖动,但为什么窗口中的 CALayer 不显示?

I tried creating a window through code and adding the CALayer to it, but I don't get why it's not showing.

NSRect rect = NSZeroRect;
    rect.size = NSMakeSize( SSRandomFloatBetween( 300.0, 200.0 ), SSRandomFloatBetween( 300.0, 200.0 ));

    NSWindow *newWin = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSWindowBackingLocationDefault defer:YES];
    [newWin setBackgroundColor: [NSColor clearColor]];
    [newWin setOpaque:NO];
    [newWin setIgnoresMouseEvents:NO];
    [newWin setMovableByWindowBackground:YES];
    [newWin makeKeyAndOrderFront:self];

    [[newWin contentView] setWantsLayer:YES];

    NSRect contentFrame = [[newWin contentView] frame];
    CALayer *newWinLayer = [CALayer layer];
    newWinLayer.frame = NSRectToCGRect(contentFrame);

    layer.backgroundColor=CGColorCreateGenericGray(0.0f, 0.5f);
    layer.borderColor=CGColorCreateGenericGray(0.756f, 0.5f);
    layer.borderWidth=5.0;

        // Calculate random origin point
    rect.origin = SSRandomPointForSizeWithinRect( rect.size, [window frame] );

        // Set the layer frame to our random rectangle.
    layer.frame = NSRectToCGRect(rect);
    layer.cornerRadius = 25.0f;
  [newWinLayer addSublayer:layer];

Window is linked to a big window, with a semi-transparent (black filled) window that is resized to fill the screen.

I've made the window draggable, but why isn't the CALayer in the window showing?

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