【发布时间】:2012-01-25 09:25:45
【问题描述】:
我很难在我的代码中找到缺失的部分。我有一个派生自 UIView(customView) 的类,还有一个派生自 CALayer (customLayer) 的类并实现了 CALayer 的 drawLayer:inContext 委托方法class.I 这样做是因为我想将 customLayer 用作剪贴蒙版,因此我需要 drawLayer: inContext 方法,该方法在我启动应用程序时不会被调用。 这是我的代码的 sn-p
@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);
}
我真的不知道发生了什么。任何建议将不胜感激。
【问题讨论】:
标签: objective-c core-graphics calayer quartz-2d