开发者

CALayer - clear internal rendering context in favor of custom drawing code

开发者 https://www.devze.com 2023-02-19 15:48 出处:网络
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/border

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 开发者_Python百科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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号