委托方法drawLayer:未调用inContext

发布于 2024-12-28 18:02:24 字数 1145 浏览 1 评论 0原文

我正在努力寻找代码中缺失的部分。我有一个派生自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-01-04 18:02:24

根据评论中的对话进行更新

首先,这一行:

CustomLayer *customLayer= [CALayer layer];     

需要使用您的 CustomLayer:

CustomLayer *customLayer= [CustomLayer layer];     

也就是说,将 customLayer 设置为其自己的委托有点奇怪,您实际上可能会遇到阻止这种情况的代码。

您是否尝试过使用 - (void)drawInContext:(CGContextRef)ctx 方法?通常,每个 CALayerDelegate 方法都有一个相应的 CALayer 方法,您可以在子类中重写该方法。

-(void) drawInContext:(CGContextRef)ctx
{
    NSLog(@"drawLayer:inContext");
    CGContextBeginPath (ctx);
    CGContextAddEllipseInRect(ctx, self.frame);
    CGContextClosePath (ctx);
    CGContextClip (ctx);

    // Note: this is where your original code ended, you have successfully set up a clipping path, but you haven't drawn anything to actually get clipped!
    CGContextSetRGBFillColor(context, 0, 0.6, 0.2, 0.4);
    CGContextFillRect(context, self.bounds);
}

另外,您是否设置 CustomLayer 实例的框架(边界/位置//中心)?我看到您将其添加到图层层次结构中,但尺寸为默认的 0,0。

Updating based on conversation in comments

First, this line:

CustomLayer *customLayer= [CALayer layer];     

needs to use your CustomLayer:

CustomLayer *customLayer= [CustomLayer layer];     

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.

-(void) drawInContext:(CGContextRef)ctx
{
    NSLog(@"drawLayer:inContext");
    CGContextBeginPath (ctx);
    CGContextAddEllipseInRect(ctx, self.frame);
    CGContextClosePath (ctx);
    CGContextClip (ctx);

    // Note: this is where your original code ended, you have successfully set up a clipping path, but you haven't drawn anything to actually get clipped!
    CGContextSetRGBFillColor(context, 0, 0.6, 0.2, 0.4);
    CGContextFillRect(context, self.bounds);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文