ios - 为什么重写 UIView.drawRect 会出现 "calling -display has no effect"消息?

标签 ios objective-c uiview metal

注意:我不寻求解决实际问题。我出于好奇问。

我有一个自定义 UIView我在应用程序的主视图中使用的子类。它有一个 Metal 层类:

+ (id)layerClass
{
    return [CAMetalLayer class];
}

我注意到如果我覆盖 -(void)drawRect:(CGRect)rect方法,错误消息开始出现:

<CAMetalLayer: 0x17005dc70>: calling -display has no effect.

更有趣的是,即使我这样覆盖它,它也会出现:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}

据我所知,这根本不可能有任何区别,因为根据 OOP 的基本原则,这样的覆盖(仅调用基类)不应该修改程序的行为。

这个错误消息到底是什么意思?为什么会出现?

最佳答案

UIView API 而且不是一个很好的 API。 UIViewCALayer 之间的交互比较复杂。有趣的是,系统会检查您是否正在覆盖 drawRect。这就是 UIView 文档中出现此警告的原因:

drawRect: - Implement this method if your view draws custom content. If your view does not do any custom drawing, avoid overriding this method.

不需要调用[super drawRect:rect]。父类(super class)不进行实际绘图。如果您不想绘制任何东西,请完全删除该方法。如果您想在图层上绘制自定义内容,请不要调用 super(仅当您知道父类(super class)也已覆盖 drawRect 时才调用 super)。

If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.

在你的情况下,我相信调用 [super drawRect] 做了一些不应该发生的事情。

关于ios - 为什么重写 UIView.drawRect 会出现 "calling -display has no effect"消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300154/

相关文章:

ios - 如何通过更改背景图像和更改UIImageView的位置进行动画处理

objective-c - 在 iOS 中切换搜索栏

swift - 如何使用大量初始化程序快速创建自定义 UIView 类?

iphone - 如何使用ViewController类获取旋转动画?

ios - Objective-C中NSXMLParser解析数字和汉字

ios - Swift 3 - 如何截取特定 UITableView 行的屏幕截图

ios - 是否有比 Objective-C 中的委托(delegate)更简洁的方法来处理异步 http 请求?

ios - subview 调整大小至全屏

ios - 我陷入了初始化 UIView 子类的困境

iphone - 不使用 superview alpha 值更新 subview