委托方法drawLayer:未调用inContext
我正在努力寻找代码中缺失的部分。我有一个派生自 UIView(customView) 的类,还有一个派生自 CALayer (customLayer) 的类并实现 CALayer 类的 drawLayer:inContext 委托方法。我这样做是因为我想使用 customLayer 作为剪贴蒙版,因此我需要 drawLayer: inContext 方法,该方法在启动应用程序时不会被调用。 的片段
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor lightGrayColor];
// Initialization code
CustomLayer *customLayer= [CALayer layer];
customLayer.backgroundColor= [UIColor colorWithRed:0 green:0.6 blue:0.2 alpha:0.4].CGColor;
customLayer.frame = self.bounds;
customLayer.delegate=customLayer;
[customLayer setNeedsDisplay];
[self.layer addSublayer:customLayer];
}
return self;
}
这是我的代码和 customLayer 实现
@implementation CustomLayer
-(void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
NSLog(@"drawLayer:inContext");
CGContextBeginPath (ctx);
CGContextAddEllipseInRect(ctx, self.frame);
CGContextClosePath (ctx);
CGContextClip (ctx);
}
:我真的不知道发生了什么。任何建议将不胜感激。
I'm struggling to find the missing piece in my code.I have a class which derives from UIView(customView) and also have a class that derives from CALayer (customLayer) and implements the drawLayer:inContext delegate method of the CALayer class.I do that because I want to use the customLayer as a clipping mask and so I need the drawLayer: inContext method which is not called when I start the application.
Here is a snippet of my code
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor lightGrayColor];
// Initialization code
CustomLayer *customLayer= [CALayer layer];
customLayer.backgroundColor= [UIColor colorWithRed:0 green:0.6 blue:0.2 alpha:0.4].CGColor;
customLayer.frame = self.bounds;
customLayer.delegate=customLayer;
[customLayer setNeedsDisplay];
[self.layer addSublayer:customLayer];
}
return self;
}
and the customLayer implementation:
@implementation CustomLayer
-(void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
NSLog(@"drawLayer:inContext");
CGContextBeginPath (ctx);
CGContextAddEllipseInRect(ctx, self.frame);
CGContextClosePath (ctx);
CGContextClip (ctx);
}
I really can't figure out what is going on.Any advices will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论中的对话进行更新
首先,这一行:
需要使用您的 CustomLayer:
也就是说,将 customLayer 设置为其自己的委托有点奇怪,您实际上可能会遇到阻止这种情况的代码。
您是否尝试过使用
- (void)drawInContext:(CGContextRef)ctx
方法?通常,每个 CALayerDelegate 方法都有一个相应的 CALayer 方法,您可以在子类中重写该方法。另外,您是否设置 CustomLayer 实例的框架(边界/位置//中心)?我看到您将其添加到图层层次结构中,但尺寸为默认的 0,0。
Updating based on conversation in comments
First, this line:
needs to use your CustomLayer:
That said, setting the customLayer as its own delegate is a bit odd, you may actually be running into code preventing this.
Have you tried using the
- (void)drawInContext:(CGContextRef)ctx
method? Generally, each CALayerDelegate method has a corresponding CALayer method that you can override in subclasses.Also, are you setting the frame (bounds/position//center) of the CustomLayer instance? I see you adding it to the layer hierarchy, but at the default 0,0 size.