CALayer - 清晰的内部渲染上下文有利于自定义绘图代码

发布于 2024-10-26 15:20:27 字数 1774 浏览 3 评论 0原文

当子类化 CALayer 并实现 drawInContext 方法时,我会假设我在其中所做的任何绘图都会显示出来,但是如果我设置(例如)borderWidth/borderColor,那么 CALayer 将首先在它自己上绘制一个边框我的自定义绘图代码。

这是 CALayer 子类:

@implementation MyCustomCALayer

- (id)init  
{  
    self = [super init];  

    if(self)  
    {  
        [self setNeedsDisplayOnBoundsChange:YES];  
    }  

    return self;  
}  

- (void)drawInContext:(CGContextRef)context  
{  
    CGRect rect = CGContextGetClipBoundingBox(context);  

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
    CGContextFillRect(context, rect);  
}  

@end  

在 UIView 中创建,如下所示:

- (void)ensureLayer
{
如果(我的图层)
返回;

    myLayer = [[[MyCustomCALayer alloc] init] autorelease];  
    myLayer.borderColor = [UIColor greenColor].CGColor;  
    myLayer.borderWidth = 1;  
    myLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);  
}  

- (void)layoutSublayersOfLayer:(CALayer *)layer  
{  
    [super layoutSublayersOfLayer:layer];  
    [self ensureLayer];  

    if(![[layer.sublayers objectAtIndex:0] isEqual:myLayer])  
        [layer insertSublayer:myLayer atIndex:0];  
}  

发生了什么,MyCustomCALayer 用红色填充一个矩形,这是我期望看到的,没有别的,因为我已经实现了drawInContext 方法,但我看到的是红色顶部有绿色边框的矩形,总是在顶部,我已经尝试了几乎所有我能想到的组合来摆脱正在绘制的绿色边框,但无法弄清楚。

我的理由是我想使用 CALayer 的 borderWidth 和 borderColor 以及其他属性,而不是创建自己的属性,因为我需要绘制的代码包含边框、填充等......但是我需要渲染做的并不是简单的造型。所以我发现解决这个问题的唯一方法是将 borderWidth 设置为 0 并将我自己的属性添加到我的子类中,例如 myBorderWidth,这是荒谬的。

这是使用最新的 iOS SDK 完成的,但我想 Mac 上也是一样的。

希望这是有道理的,有什么想法吗?

谢谢!

When subclassing an CALayer and implementing the drawInContext method, I would assume that any drawing I do within there is all that will show up, but instead if I set (for example) borderWidth/borderColor then CALayer will draw a border on it's own above all my custom drawing code.

This is a CALayer subclass:


@implementation MyCustomCALayer

- (id)init  
{  
    self = [super init];  

    if(self)  
    {  
        [self setNeedsDisplayOnBoundsChange:YES];  
    }  

    return self;  
}  

- (void)drawInContext:(CGContextRef)context  
{  
    CGRect rect = CGContextGetClipBoundingBox(context);  

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
    CGContextFillRect(context, rect);  
}  

@end  

Created in a UIView something like:


- (void)ensureLayer
{
if(myLayer)
return;

    myLayer = [[[MyCustomCALayer alloc] init] autorelease];  
    myLayer.borderColor = [UIColor greenColor].CGColor;  
    myLayer.borderWidth = 1;  
    myLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);  
}  

- (void)layoutSublayersOfLayer:(CALayer *)layer  
{  
    [super layoutSublayersOfLayer:layer];  
    [self ensureLayer];  

    if(![[layer.sublayers objectAtIndex:0] isEqual:myLayer])  
        [layer insertSublayer:myLayer atIndex:0];  
}  

What happens, is the MyCustomCALayer fills a rectangle with red, this is what I would expect to see and nothing else, since i've implemented the drawInContext method, but instead I see a red rectangle with a green border on top, always on top, i've tried just about every combination I can think of to get rid of the green border being drawn and cannot figure it out.

My reasoning is I would like to use the borderWidth and borderColor and other properties of the CALayer instead of creating my own properties, because the code that I need to draw contains a border, a fill, etc... but the rendering I need to do is not a simple shape. So the only way i've found around this is to set the borderWidth to 0 and add my own property to my subclass, like myBorderWidth, which is ridiculous.

This is done with the latest iOS SDK, but i'd imagine it's the same for Mac.

Hope this makes sense, any thoughts?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情栀口红 2024-11-02 15:20:27

你运气不好; CoreAnimation 不支持覆盖其基本图层属性的渲染实现。请提交错误

You’re out of luck; CoreAnimation doesn’t support overriding its implementation of rendering for the basic layer properties. Please do file a bug.

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