CALayer委托方法drawLayer没有被调用
我的对象是 NSObject 的子类,具有 CALayer 实例变量。 我用它来绘制和缓存内容,并将其委托设置为我的对象。
但由于某种原因,drawLayer:inContext:
方法永远不会被调用。 而 actionForLayer:forKey:
委托方法确实被调用,这意味着委托已在对象的 init 方法中使用 layer.delegate = self
正确设置。
关于阻止调用我的图层绘制方法 drawLayer:inContext:
的任何建议?
我经常被称为[layer setNeedDisplay]
。 所以我想这是一些根本性的错误。
My object a sub class of NSObject has CALayer instance variable. I use it for drawing and caching content with its delegate set to my object.
But for some reason drawLayer:inContext:
method NEVER gets called. Whereas actionForLayer:forKey:
delegate method does get called implying the delegate is getting set properly with layer.delegate = self
in the object's init method.
Any suggestions on what is preventing my layer drawing method drawLayer:inContext:
from getting called ?
I am called the [layer setNeedDisplay]
often. So I guess it is some fundamental error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您最终将 CALayer 与 UIView 一起使用,则委托必须是视图对象本身:
来自 iOS CALayer 文档:
“在 iOS 中,如果图层与 UIView 对象关联,则必须将此属性设置为视图拥有该层。”
If you're eventually using the CALayer with a UIView then the delegate must be the view object itself:
From the iOS CALayer documentation:
"In iOS, if the layer is associated with a UIView object, this property must be set to the view that owns the layer."
必须调用图层对象的
setNeedsDisplay
。 简单地将图层添加为子图层并不适合您。 这是从 Ray Wenderlich 的 CALayer 教程 获得的。The layer object's
setNeedsDisplay
must be called. Simply adding the layer as a sublayer does not do that for you. Got this from Ray Wenderlich's CALayer tutorial.如果您的框架是 CGRectZero 或离屏,则不会调用
drawLayer:inContext:
。 此外,如果您的 CALayer 未附加到现有的屏幕图层,则无论您调用 setNeedsDisplay 多少次,它都永远不会绘制drawLayer:inContext:
won't get called if your frame is CGRectZero or offscreen. Also, if your CALayer isn't attached to an existing onscreen layer, it will never draw, no matter how many times you callsetNeedsDisplay
实现一个空的
drawRect:
取自ZoomingPDFViewer项目:-
Implement an empty
drawRect:
Taken from the ZoomingPDFViewer project:-
如果您有一个多线程应用程序,其中后台处理需要更新 CALayer,则必须在主线程中调用 setNeedsDisplay
If you have a multi-threaded app where background processing drives the need to update the CALayer, you must call setNeedsDisplay in the main thread