[英]Draggable CALayer
用户可以使CALayer拖拽的任何方式吗? 如果是这样,怎么样?
(在Cocoa - Mac中)
图层无法自行接收鼠标事件。 您必须在视图中查看事件处理或查看包含该图层的控制器。
如果mouseDragged:
事件来自某个图层(请参阅-[CALayer hitTest:]
和-[CALayer containsPoint:]
进行测试),请相应地调整图层的position
。 您可能希望禁用隐式动画以使图层立即跟随鼠标指针(而不是因为position属性的动画而滞后一点):
[CATransaction begin];
[CATransaction setDisableActions:YES];
layer.position = ...;
[CATransaction commit];
我尝试通过代码创建一个窗口并将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不显示?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.