你如何覆盖UIView的+(id)层;类方法? [英] How do you override UIView's +(id)layer; class method?

查看:124
本文介绍了你如何覆盖UIView的+(id)层;类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档中读到UIView的'layer'属性是只读的,你必须覆盖UIView的

I read in the documentation that a UIView's 'layer' property is read only and that you must override UIView's

+ (id)layer;

访问图层样式属性的类方法。

class method to access the layer's styling properties.

是否有任何覆盖此方法的示例返回已应用样式属性的图层/视图?

Are there any examples of overriding this method to return a layer/view with styling properties already applied?

推荐答案

如果您只想在分配给UIView的默认CALayer上设置backgroundColor,opacity等属性,您可以随时使用以下内容在UIView层上设置这些属性:

If you just want to set properties like backgroundColor, opacity, etc. on the default CALayer that's assigned to the UIView, you can set those on the UIView's layer at any time using something like the following:

view.layer.opacity = 0.0f;

你唯一需要覆盖 - (CALayer)图层方法的时候就是你想要的返回自定义CALayer子类。我相信在iPhone上,Apple建议您改写类方法layerClass。这将返回初始化自定义视图时要创建的CALayer子类。例如,

The only time that you would need to override the - (CALayer)layer method would be if you wanted to return a custom CALayer subclass. I believe that on the iPhone Apple recommends you override the class method layerClass instead. This will return the CALayer subclass to be created when initializing your custom view. For example,

+ (Class) layerClass 
{
    return [CAEAGLLayer class];
}

导致您的UIView子类使用OpenGL层作为其支持。

causes your UIView subclass to use an OpenGL layer for its backing.

这篇关于你如何覆盖UIView的+(id)层;类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆