CALayer、anchorPoint 和图层层次结构
我尝试使用图层层次结构中的锚点来移动图层及其子图层。不幸的是,子层没有与根层一起移动。在自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论