CALayer addsublayer 不添加子层
我遇到了最奇怪的问题:有时向 CALayer 添加子层不起作用([layer addSublayer:child]
),即子层计数保持为 0 并且没有任何内容绘制到屏幕上。
有时它确实有效并且子层会按照我的预期显示在屏幕上。如果没有,通常执行干净的构建可以修复它,但并非总是如此。我还尝试从终端运行我的应用程序的两个实例,通常第二个实例显示有问题的行为。
以下是(缩写)代码:
NSLog(@"Retain count before: %d", [childLayer retainCount]);
// [parentLayer insertSublayer:childLayer atIndex:0]; // same problem here
[parentLayer childLayer];
NSLog(@"Retain count after: %d layer: %@", [childLayer retainCount], childLayer);
NSLog(@"Sublayercount: %d", [[parentLayer sublayers] count]);
子图层是一个基本图层,背景设置为 RGB 颜色。
调试输出:
layer: <CALayer: 0x1982b0> X: 50.00 Y: -90.00 width: 200.00 height: 200.00
Retain count before: 2
Retain count after: 2 layer: <CALayer: 0x1982b0>
Sublayercount: 0
当它正常工作时,上面的输出显示保留计数正确增加到 3
,子层计数增加到 1
。
CALayers 有什么特别之处吗? addSublayer
是否依赖其他东西/执行子层的有效性检查?
有人可以建议一种调试方法吗?有没有办法使用调试器进入 addSublayer
?
谢谢!标记。
I ran into the strangest of problems: sometimes adding a sublayer to a CALayer does not work ([layer addSublayer:child]
), i.e. the sublayer count remains at 0 and nothing is drawn to the screen.
Sometimes it does work and the child layer shows up on the screen as I would have expected. When it does not, usually performing a clean build fixes it, but not always. I also experimented with running two instances of my app from the Terminal, usually the second instance shows the problematic behavior.
Here is the (abbreviated) code:
NSLog(@"Retain count before: %d", [childLayer retainCount]);
// [parentLayer insertSublayer:childLayer atIndex:0]; // same problem here
[parentLayer childLayer];
NSLog(@"Retain count after: %d layer: %@", [childLayer retainCount], childLayer);
NSLog(@"Sublayercount: %d", [[parentLayer sublayers] count]);
The child layer is a basic layer with the background set to a RGB color.
Debug output:
layer: <CALayer: 0x1982b0> X: 50.00 Y: -90.00 width: 200.00 height: 200.00
Retain count before: 2
Retain count after: 2 layer: <CALayer: 0x1982b0>
Sublayercount: 0
When it does work, the above output shows the retain count being increased correctly to 3
and the sublayercount to 1
.
Is there something special about CALayers? Does addSublayer
rely on something else/perform validity checks of the sublayer?
Can anybody suggest a way to debug this? Is there a way how I can step into addSublayer
with the debugger?
Thanks! Mark.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于想通了。有时,只是谈论(好吧,发帖)这个问题会有所帮助。
事实证明,这是我初始化保存图层的自定义视图的方式的问题。对应用控制器和视图的
awakeFromNib
调用之间存在竞争条件。应用程序控制器将加载自定义层,但有时自定义视图尚未初始化。解决方案是将应用控制器中视图的初始化代码从
awakeFromNib
移至appliationDidFinishLaunching
。I finally figured it out. Sometimes it helps to just talk (well, post) about the issue.
It turned out to be a problem with the way I initialized the custom view that holds the layers. There was a race condition between the
awakeFromNib
calls to the app controller and the view. The app controller would load the custom layers, but sometimes the custom view was not yet initialized.The solution was to move the initialization code for the view in the app controller from
awakeFromNib
toappliationDidFinishLaunching
.retainCount
没用;不要称呼它......很高兴你解决了这个问题。
retainCount
is useless; don't call it....Glad you figured out the problem.