gpt4 book ai didi

ios - CALayers 从不称其为委托(delegate)的 drawLayer :inContext: even after [layer setNeedsDisplay] and [layer display]

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:03 24 4
gpt4 key购买 nike

我不明白为什么我创建的 CALayer 没有调用它们的 drawLayer 方法。我已经为他们创建了一个 drawLayer 委托(delegate)对象,但它从未被调用过。来自 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]。

为什么图层拒绝绘制?

最佳答案

层框架需要是非零的。这解决了问题:

layer.frame = self.bg.bounds;
[layer setNeedsDisplay];
[self.bg.layer addSublayer:layer];

关于ios - CALayers 从不称其为委托(delegate)的 drawLayer :inContext: even after [layer setNeedsDisplay] and [layer display],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15988460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com