ios – CALayers永远不会调用它的委托的drawLayer:inContext:甚至在[layer setNeedsDisplay]和[layer display]之后

前端之家收集整理的这篇文章主要介绍了ios – CALayers永远不会调用它的委托的drawLayer:inContext:甚至在[layer setNeedsDisplay]和[layer display]之后前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法弄清楚为什么我正在创建的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之前和之后尝试了[图层显示].我曾尝试使用和不使用[图层显示]和[图层displayIfNeeded].

为什么图层拒绝绘制?

解决方法

图层框架必须为非零.这解决了这个问题:
layer.frame = self.bg.bounds;
[layer setNeedsDisplay];
[self.bg.layer addSublayer:layer];
原文链接:https://www.f2er.com/iOS/334612.html

猜你在找的iOS相关文章