gpt4 book ai didi

objective-c - CALayer 不显示

转载 作者:行者123 更新时间:2023-12-03 17:06:58 26 4
gpt4 key购买 nike

在尝试制作可拖动的 CALayer ( Question on Stack Overflow here ) 时,我尝试通过代码创建一个窗口并向其中添加一个 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 没有显示?

最佳答案

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:

Discussion The order that setWantsLayer: and setLayer: are called is important, it makes the distinction between a layer-backed view, and a layer-hosting view.

A layer-backed view is a view that is backed by a Core Animation layer. Any drawing done by the view is the cached in the backing layer. You configured a layer-backed view by simply invoking setWantsLayer: with a value of YES. The view class will automatically create the a backing layer for you, and you use the view class’s drawing mechanisms. When using layer-backed views you should never interact directly with the layer.

A layer-hosting view is a view that contains a Core Animation layer that you intend to manipulate directly. You create a layer-hosting view by instantiating an instance of a Core Animation layer class and setting that layer using the view’s setLayer: method. After doing so, you then invoke setWantsLayer: with a value of YES. When using a layer-hosting view you should not rely on the view for drawing, nor should you add subviews to the layer-hosting view.

我相信您这样做的方式是尝试创建一个图层支持的 View ,您不应该像以前那样尝试与 View 的底层进行交互。您想要的是层托管的多样性。

关于objective-c - CALayer 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5139864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com