在调整父nsview大小时,如何将Calayer保持在相同的位置?
想象一个小红色框(CALayer
实例)绘制在其父层(托管 NSView
的层的根层)的左下角。
当父视图的框架发生变化时,红色框应保持在屏幕上的相同位置。我通过调整它相对于父视图左下角的位置来做到这一点。
问题是,在某些情况下会出现闪烁,我可以看到在扩展帧的左下角绘制了红色框图层,然后才将其显示在正确的位置。
我假设将帧和位置更改包装到一个 CATransaction 中将使这两项更改一起进行,但这并不总是有效(文档说通过使用事务,动画将同时开始,但有时似乎仍然存在竞争条件)。
如何调整父 NSView 的框架,同时将子图层保持在其感知位置?
示例和代码:
我自己的想法:
隐藏红框图层,更新位置,再次显示
使用约束将其绑定到右角。问题是这个偏移量也可能会改变,我必须更新约束,这可能会导致相同的闪烁问题。
Imagine a small red box (CALayer
instance) drawn in the lower left corner of its parent layer (which is the root layer of a layer hosting NSView
).
When the frame of the parent view changes, the red box should remain at the same position on the screen. I do this by adjusting it's position relative to the lower left corner of the parent view .
The problem is that in some cases there is flickering and I can see the red box layer being drawn in the lower left corner of the extended frame before it is shown at the correct position.
I assumed that wrapping the frame and position change into one CATransaction
would make both changes together, but that doesn't always work (the docs say that by using a transaction the animations will start at the same time, but there still seems to be a race condition at times).
How can I adjust the frame of the parent NSView
while keeping the child layer at its perceived position?
Example and code:
My own ideas:
Hide red box layer, update the position, show it again
Use constraints to bind it to the right corner. Problem is that this offset could also change and I would have to update the constraint which could lead to the same flickering issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试删除您已经编写的使图层随视图移动的代码,并将其放在您要创建图层的位置。
Try deleting the code you have already made to have the layer move with the view and put this where you are creating the layer.