微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/iOS/334612.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐