2011-02-26 50 views

回答

4

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

如果一个mouseDragged:事件发生在图层上(请参阅-[CALayer hitTest:]-[CALayer containsPoint:]以测试此图层),请相应地调整图层的position。你可能会想禁用隐式动画有层立即跟随鼠标指针(而不是滞后有点落后了,因为位置属性的动画):

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

“UIPanGestureRecogniser”?我在Mac上。 – 2011-02-26 15:41:09

+0

对不起,我错过了那一点,并假设你在iOS上。要编辑我的答案。 – 2011-02-26 15:44:29

+0

好的,谢谢! :)这有点困难,因为我正在使用的CALayer本身就是一个窗口的子层,它主要是用代码完成的。:( – 2011-02-26 16:02:15

1

我试图创建通过代码的窗口和添加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?

+0

NSWindowBackingLocationDefault是支持的枚举类型错误,它应该来自NSBackingStoreType,例如NSBackingStoreBuffered。 – cmeub 2013-03-04 05:14:32