简体   繁体   English

可以从 UIView 复制 CALayer 吗?

[英]Possible to copy CALayer from UIView?

Here's my setup: I have a CALAyer to which I want to add sublayers.这是我的设置:我有一个 CALAyer,我想向其中添加子图层。 I create these sublayers by setting upa UILabel and then adding the UILables layer to my main layer.我通过设置 UILabel 然后将 UILables 层添加到我的主层来创建这些子层。 Of course this leaves the heavy UILabel object hovering around in the background.当然,这会让沉重的 UILabel 对象在后台徘徊。 Is it possible to get the layer with all its content from a UIView and get rid of the UIView itself?是否可以从 UIView 获取包含其所有内容的图层并摆脱 UIView 本身? I already tried this:我已经试过了:

UILabel* label;
[...]
[mainLayer addSublayer:[label.layer copy]];
[label release];

But whenever I release the UIView, the content of the layer is also removed.但是每当我释放 UIView 时,图层的内容也会被删除。 Is this even possible or does the UIView's layer always need the UIView itself to show it's content?这甚至是可能的还是 UIView 的层总是需要 UIView 本身来显示它的内容? I thought of the layer as a kind of canvas, to which the UIView paints.我认为图层是一种画布,UIView 会在其上进行绘制。 I guess I could be wrong with this assumption :)我想我的这个假设可能是错误的:)

I can't understand why you wouldn't be able to copy a layer.我不明白为什么你不能复制图层。 True a layer is an "integral part" of a UIView.真正的图层是 UIView 的“组成部分”。 But in the end it is just another object with several properties.但最终它只是另一个具有多个属性的对象。

Actually there is a method for CALayer called:其实CALayer有一个方法叫做:

- (id)initWithLayer:(id)layer

But it isn't intended to make a copy of a layer.但它并不打算制作图层的副本。 (You can read Apple's docs for the reasoning why) (您可以阅读 Apple 的文档以了解原因)

CALayer does not conform to NSCopying, so you have two options: CALayer 不符合 NSCopying,所以你有两个选择:

  1. Subclass it and implement "copyWithZone:" (and conform to NSCopying)将其子类化并实现“copyWithZone:”(并符合 NSCopying)
  2. Write a method/function that will return a "copy" of a CALayer编写一个将返回 CALayer 的“副本”的方法/函数

Regardless of which way you choose, the question you have to ask is: Which properties of the CALlayer do you want to copy?不管你选择哪种方式,你要问的问题是:你想复制CALlayer的哪些属性?

Let's say you want to copy just the contents and frame:假设您只想复制内容和框架:

CALayer copyLayer = [CALayer layer];

copyLayer.contents = theLayerToBeCopied.contents;
copyLayer.frame = theLayerToBeCopied.frame;

return copyLayer;

You could go through and copy every property of the layer, or just copy the ones you need.您可以遍历并复制图层的每个属性,或者只复制您需要的属性。 Might be a good method to put in a CALayer category.可能是放入 CALayer 类别的好方法。

It is not possible: a layer is a property of a UIView.这是不可能的:图层是 UIView 的属性。 Therefore, when you release the UIView, its layer is gone.因此,当您释放 UIView 时,它的层就消失了。 Your idea of thinking of the layer as a kind of canvas is not wrong.您将图层视为一种画布的想法并没有错。 But, the layer is an integral part of its UIView.但是,该层是其 UIView 的一个组成部分。

使用CAReplicatorLayer,您将能够完全复制该层

UIViews are not that heavy in iOS. UIViews 在 iOS 中没有那么重。 While not 100% accurate, for the most part, you can think of them as a supporting wrapper around a CALayer.虽然不是 100% 准确,但在大多数情况下,您可以将它们视为 CALayer 的支持包装器。 Unlike on Mac where a NSView doesn't have to be backed by a CALayer, that's not true in iOS.与在 Mac 上 NSView 不必由 CALayer 支持的情况不同,在 iOS 中情况并非如此。 All UIViews are CALayer-backed and that's where most of the heavy lifting is.所有UIViews 都是 CALayer 支持的,这就是大部分繁重的工作。 Apple's docs even say things like 'Don't bother animating a UIView's layer. Apple 的文档甚至说“不要费心为 UIView 的图层设置动画”。 Animate the UIView directly as it's essentially just sending it all down to the layer anyway.'直接动画 UIView 因为它本质上只是将它全部发送到层。 (Paraphrased, of course.) (当然是转述。)

Point being, unless you specifically need UILayers, just stick with working with UIViews entirely and you should be good to go.重点是,除非你特别需要 UILayers,只要坚持完全使用 UIViews 就可以了。

You can also circumvent UILabel entirely and create your text with a CATextLayer .您还可以完全绕过 UILabel 并使用CATextLayer创建文本。

UILabel really isn't a very complex control. UILabel 真的不是一个非常复杂的控件。 I suggest you give up this line of attack and learn how to draw what you want using Quartz into a fresh CGImage.我建议你放弃这条攻击线,学习如何使用 Quartz 将你想要的绘制成一个新的 CGImage。 You can then assign this to the contents property of the CALayer without having to worry about all this other stuff.然后,您可以将其分配给 CALayer 的内容属性,而不必担心所有其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM