CALayer、anchorPoint 和图层层次结构

发布于 2024-09-10 10:44:11 字数 1166 浏览 1 评论 0原文

我尝试使用图层层次结构中的锚点来移动图层及其子图层。不幸的是,子层没有与根层一起移动。在自定义 NSView 中设置我的图层层次结构,如以下代码片段所示。

CALayer * rootLayer;
rootLayer = [[CALayer layer] retain];
rootLayer.position = CGPointMake(...);
[self.layer addSublayer:rootLayer];

subLayer = [[MapLayer layer] retain];
subLayer.position = CGPointMake(...);
[rootLayer addSublayer:baseLayer];

在处理鼠标事件时,我想设置 rootLayer 的锚点以移动整个图层层次结构:

rootLayer.anchorPoint = CGPoint(0.7, 0.7);

我希望通过此调用,rootLayer 与其子图层一起移动,以便锚点位于视图的中心。发生的情况是,子层没有移动。仅当我调用时:

rootLayer.anchorPoint = CGPoint(0.7, 0.7);
subLayer.anchorPoint = CGPoint(0.7, 0.7);

各层的行为符合预期。

我认为设置根层的锚点就足够了。这在我的 OS X 的 Map 应用程序中使用。在那里我设置了视图并使用锚点来移动整个地图。由于自定义 NSView 中只有一个子层,应用程序的行为符合预期。

提前致谢。

更新:与同事讨论后,我设置了 rootLayer 的边界,现在它可以工作了。

CALayer * rootLayer;
rootLayer = [[CALayer layer] retain];
rootLayer.bounds = CGRectMake(0, 0, 256, 256);
rootLayer.position = CGPointMake(...);
[self.layer addSublayer:rootLayer];

// ...

I try to use the anchorPoint in a layer hierarchy to move a layer with its sublayers. Unfortunately the sublayer did not move together with the root layer. In a custom NSView is set up my layer hierarchy like in the following snippet.

CALayer * rootLayer;
rootLayer = [[CALayer layer] retain];
rootLayer.position = CGPointMake(...);
[self.layer addSublayer:rootLayer];

subLayer = [[MapLayer layer] retain];
subLayer.position = CGPointMake(...);
[rootLayer addSublayer:baseLayer];

While handling a mouse event I want to set the anchorPoint of the rootLayer to move the whole layer hierarchy:

rootLayer.anchorPoint = CGPoint(0.7, 0.7);

I expect from this call, that the rootLayer moves together with its subLayer so that the anchor point is at the center of the view. What happens is, that the sublayer did not move. Only when I call:

rootLayer.anchorPoint = CGPoint(0.7, 0.7);
subLayer.anchorPoint = CGPoint(0.7, 0.7);

the layers behave as expected.

I thought setting the anchor point of the root layer would be enough. This is used in my Map application for OS X. There I set up the view and use the anchorPoint to move the whole map. With only one sublayer in the custom NSView the application behaves as expected.

Thanks in advance.

UPDATE: After a discussion with a colleague I set the bounds of the rootLayer and now it works.

CALayer * rootLayer;
rootLayer = [[CALayer layer] retain];
rootLayer.bounds = CGRectMake(0, 0, 256, 256);
rootLayer.position = CGPointMake(...);
[self.layer addSublayer:rootLayer];

// ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文