在调整父nsview大小时,如何将Calayer保持在相同的位置?

发布于 2024-12-05 06:50:10 字数 593 浏览 0 评论 0原文

想象一个小红色框(CALayer 实例)绘制在其父层(托管 NSView 的层的根层)的左下角。

当父视图的框架发生变化时,红色框应保持在屏幕上的相同位置。我通过调整它相对于父视图左下角的位置来做到这一点。

问题是,在某些情况下会出现闪烁,我可以看到在扩展帧的左下角绘制了红色框图层,然后才将其显示在正确的位置。

我假设将帧和位置更改包装到一个 CATransaction 中将使这两项更改一起进行,但这并不总是有效(文档说通过使用事务,动画将同时开始,但有时似乎仍然存在竞争条件)。

如何调整父 NSView 的框架,同时将子图层保持在其感知位置?

示例和代码:

NSView 和 box 层

我自己的想法:

  1. 隐藏红框图层,更新位置,再次显示

  2. 使用约束将其绑定到右角。问题是这个偏移量也可能会改变,我必须更新约束,这可能会导致相同的闪烁问题。

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:

NSView and box layer

My own ideas:

  1. Hide red box layer, update the position, show it again

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别闹i 2024-12-12 06:50:10

尝试删除您已经编写的使图层随视图移动的代码,并将其放在您要创建图层的位置。

boxLayer.autoresizingMask = kCALayerMaxXMargin | kCALayerMinYMargin;

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.

boxLayer.autoresizingMask = kCALayerMaxXMargin | kCALayerMinYMargin;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文