CALayer delegate method drawLayer not getting called

99封情书 提交于 2019-12-04 00:02:41

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 call setNeedsDisplay

Implement an empty drawRect:

- (void)drawRect:(CGRect)rect {
}

Taken from the ZoomingPDFViewer project:-

UIView uses the existence of -drawRect: to determine if it should allow its CALayer to be invalidated, which would then lead to the layer creating a backing store and -drawLayer:inContext: being called. By implementing an empty -drawRect: method, we allow UIKit to continue to implement this logic, while doing our real drawing work inside of -drawLayer:inContext:

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.

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."

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!