Objective c 卡莱尔没有露面
在尝试制作一个可拖动的CALayer()时,我尝试通过代码创建一个窗口并向其中添加一个CALayer,但我不明白为什么它没有显示出来Objective c 卡莱尔没有露面,objective-c,cocoa,core-animation,Objective C,Cocoa,Core Animation,在尝试制作一个可拖动的CALayer()时,我尝试通过代码创建一个窗口并向其中添加一个CALayer,但我不明白为什么它没有显示出来 NSRect rect = NSZeroRect; rect.size = NSMakeSize( SSRandomFloatBetween( 300.0, 200.0 ), SSRandomFloatBetween( 300.0, 200.0 )); NSWindow *newWin = [[NSWindow alloc] initWithCo
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];
“窗口”链接到一个大窗口,带有一个半透明(黑色填充)窗口,该窗口可调整大小以填充屏幕
我已经把窗户拉起了,但是为什么窗户上的玻璃窗不露出来呢
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];
// you don't want to do this yet
// [[newWin contentView] setWantsLayer:YES];
NSRect contentFrame = [[newWin contentView] frame];
CALayer *newWinLayer = [CALayer layer];
newWinLayer.frame = NSRectToCGRect(contentFrame);
换行,也是内存管理问题:
// NOTE: remember that the following 2 *Create* methods return
// results that need to be released, unless you're using Garbage-Collection
// Also, I'm guessing that `layer` is created somewhere?
CALayer *layer = [CALayer layer];
CGColorRef backgroundCol = CGColorCreateGenericGray(0.0f, 0.5f);
CGColorRef borderCol = CGColorCreateGenericGray(0.756f, 0.5f);
layer.backgroundColor=backgroundCol;
layer.borderColor=borderCol;
CGColorRelease(backgroundCol); CGColorRelease(borderCol);
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];
NSView *view = [newWin contentView];
// the order of the following 2 methods is critical:
[view setLayer:newWinLayer];
[view setWantsLayer:YES];
有关详细信息,请参阅NSView的文档
讨论
命令
setWantsLayer:
和setLayer:
是
打电话是重要的,它使
层与层之间的区别
视图和图层宿主视图
以图层为背景的视图是
由核心动画层支持。任何
视图完成的绘图是缓存的
在背衬层。您配置了一个
通过简单地调用
setWantsLayer:
值为YES
。
视图类将自动
为您创建一个背衬层,
然后使用view类的图形
机制使用层备份时
你不应该与他人互动的观点
直接使用图层
图层宿主视图是指
包含一个核心动画层
你打算直接操纵。你
通过创建图层宿主视图
实例化核心的实例
动画层类和设置
使用视图的setLayer:
方法。这样做之后,你就
使用值调用setWantsLayer:
对使用图层宿主视图时
你不应该依赖于这个观点来进行研究
也不应添加子视图
到图层宿主视图
我相信你这样做的方式是试图创建一个以层为基础的视图,在这里你不应该像以前那样尝试与视图的底层交互。你想要的是层托管品种