开发者

Can I set layer.transform on an NSView with subviews?

开发者 https://www.devze.com 2023-03-05 12:51 出处:网络
I\'d like to have an NSView with subviews (images, text, etc.) and then apply a 3d transform to it using myView.layer.transform = myTransform.

I'd like to have an NSView with subviews (images, text, etc.) and then apply a 3d transform to it using myView.layer.transform = myTransform.

After reading the docs I am confused as to whether th开发者_运维知识库is is allowed. If it's a layer-backed view, then I am not supposed to be interacting with the layer directly, so setting the transform seems wrong.

If it's a layer-hosting view, then I'm not supposed to have subviews.

If I want to set layer.transform, does that mean I have to set up all my drawing through CALayer, and stop using subviews?


I just set up a CALayer animation on an NSView that has nested subviews, NSScrollViews, and it works OK. I think the restriction is on overlapping, superimposed views. (The problem with the latter is that there is no specified drawing order; it's unpredictable which view will be displayed on top.)

But if you want to apply the transform to the subviews as well, you would have to set up layers for them separately. Or, yes, you could dump the subviews and put everything on the CALayers of a single view. You can control their size and placement using the bounds and position properties. (Note position is from center, not lower left, unless you change the anchor point.)

If you're doing layer-hosting, do not apply your transform to the root layer (view.layer). Instead, make a new CALayer, add contents to it, add the transform to it, and apply it as a sublayer to the root layer. Avoid working with the root layer directly.

Quick sample of layer-hosting setup:

// Set up the root layer.
[[self.aViewController view] setLayer:[CALayer layer]];
[[self.aViewController view] setWantsLayer:YES];
// Set up a sublayer.
CALayer *sublayer = [CALayer layer];
[self.aViewController.view.layer addSublayer:sublayer];
// Repeat if you need additional sublayers. There's a name property if you need to distinguish between them.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号