我不明白为什么我正在创建的 CALayer 没有调用他们的 drawLayer 方法。我为他们创建了一个 drawLayer 委托对象,但它永远不会被调用。从 UIView 子类:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIView *bg = [[UIView alloc] initWithFrame:self.bounds];
// I have a property called 'bg' to reference the child UIView
self.bg = bg;
[self addSubview:self.bg];
// an NSObject subclass I use as a CALayer delegate
MyLayerDelegate *drawer = [MyLayerDelegate new];
self.drawer = drawer;
CAGradientLayer *layer = [CAGradientLayer layer];;
layer.delegate = self.drawer;
layer.name = @"bg";
[self.bg.layer addSublayer:layer];
[layer setNeedsDisplay];
[layer display];
}
return self;
}
在 self.drawer 我有:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
NSLog(@"layer called drawLayer");
// Some drawing goes here
}
我尝试了很多不同的东西。我尝试在 self.bg.layer 上设置 setNeedsDisplay,我在 self.bg 上尝试过 setNeedsDisplay,我在 addSublayer 之前和之后尝试过 [layer setNeedsDisplay],我在 addSublayer 之前和之后尝试过 [layer display]。我尝试过使用和不使用 [layer display] 和 [layer displayIfNeeded]。
为什么图层拒绝绘制?