gpt4 book ai didi

iphone - CATiledLayer 上的 CALayer,drawLayer :inContext never called

转载 作者:行者123 更新时间:2023-11-29 13:34:32 26 4
gpt4 key购买 nike

我有一个使用 CATiledLayer 显示 PDF 页面的 UIView,现在我想在 TiledLayer 上添加另一个 CALayer 来绘制一些注释,请参见下面的代码。

    + (Class)layerClass
{
return [TiledLayer class];
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
TiledLayer *tiledLayer = (id)self.layer;
tiledLayer.backgroundColor = [UIColor whiteColor].CGColor;
NSAssert([tiledLayer isKindOfClass:[TiledLayer class]], @"self.layer must be CATiledLayer");

drawingLayer = [CALayer layer];
drawingLayer.frame = frame;
//its crashing if set the delegate, if not drawLayer is never called.
[drawingLayer setDelegate:self];
[self.layer addSublayer:self.drawingLayer];

}

}

- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
if(layer == [self layer])
{
[self drawPDFPage];
[drawingLayer setNeedsDisplay];
}
else if(layer == drawingLayer)
{
//this one is never called.
[self drawSomethinghere];
}
}

最佳答案

UIView 使用 -drawRect: 的存在来确定它是否应该允许其 CALayer 失效,这将导致图层创建后备存储并调用 -drawLayer:inContext:。

实现一个空的 -drawRect: 方法允许 UIKit 继续实现这个逻辑,同时在 -drawLayer:inContext: 中完成真正的绘图工作。

只需添加到您的类实现中:

-(void)drawRect:(CGRect)r
{

}

并且您的 drawLayer 方法应该可以工作。

关于iphone - CATiledLayer 上的 CALayer,drawLayer :inContext never called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10936233/

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