I do not understand why you cannot copy the layer. The true layer is an "integral part" of the UIView. But in the end, this is just another object with several properties.
Actually there is a method for CALayer called:
- (id)initWithLayer:(id)layer
But it is not intended to create a copy of the layer. (You can read Apple docs for reasons)
CALayer is not NSCopying compliant, so you have two options:
- Subclass and implementation of "copyWithZone:" (and conform to NSCopying)
- Write a method / function that will return a "copy" of CALayer
No matter which way you choose, the question you ask is: What CALlayer properties do you want to copy?
Say you want to copy only the content and frame:
CALayer copyLayer = [CALayer layer]; copyLayer.contents = theLayerToBeCopied.contents; copyLayer.frame = theLayerToBeCopied.frame; return copyLayer;
You can go through and copy all the properties of the layer or just copy the ones you need. Perhaps this is a good way to include the CALayer category.
Corey floyd
source share