如何子类化 CALayer 以返回预先设置样式的图层?
如何子类化 CALayer 以便返回已设置样式属性的图层?
这样:
MyCustomLayer *layer = [MyCustomLayer layer];
将创建一个具有这些属性的图层;
layer.backgroundColor = sweetBackgroundColor.CGColor;
layer.borderColor = sweetBorderColor.CGColor;
layer.borderWidth = 2.0;
layer.cornerRadius = 8.0;
这样我就可以设置它的框架并将其添加到树中。
但我想我的问题实际上可能是,如何子类化一个对象,以便它具有在创建时已设置的属性。 可能的?
How do you subclass CALayer so that you return a layer with styling properties already set?
So that:
MyCustomLayer *layer = [MyCustomLayer layer];
would create a layer with these properties;
layer.backgroundColor = sweetBackgroundColor.CGColor;
layer.borderColor = sweetBorderColor.CGColor;
layer.borderWidth = 2.0;
layer.cornerRadius = 8.0;
So that I can just set its frame and add it to the tree.
But I guess my question really could just be, how do you subclass an object so that it has properties already set at creation. Possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在子类中覆盖 -init (CALayer 的指定初始值设定项),并在覆盖中设置这些属性。 任何创建方法,包括 -layer,最终都应该通过指定的初始化器进行路由,这意味着即使对于方便的构造函数,您也会进行额外的设置。
Just override -init (the designated initializer for CALayer) in your subclass, and set those properties in your override. Any creation method, including -layer, should end up routing through the designated initializer, meaning your extra setup will happen even for convenience constructors.