为什么drawRect:在不调用[super drawrect:rect]的情况下工作? [英] Why does drawRect: work without calling [super drawrect:rect]?

查看:361
本文介绍了为什么drawRect:在不调用[super drawrect:rect]的情况下工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一种视图中,我重写了drawRect:即使不调用[super drawrect:rect],它也可以工作。这是如何运作的?

I'm overriding drawRect: in one of my views and it works even without calling [super drawrect:rect]. How does that work?

- (void)drawRect:(CGRect)rect{  
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeDestinationOver);
    [[UIColor clearColor] setFill];
    UIRectFill(CGRectMake(0,0,100,100));
}


推荐答案

摘自文档:


此方法的默认实现不执行任何操作。子类
使用Core Graphics和UIKit等技术绘制其
视图的内容应覆盖此方法,并在此处实现其绘图
代码。

"The default implementation of this method does nothing.Subclasses that use technologies such as Core Graphics and UIKit to draw their view’s content should override this method and implement their drawing code there"

也来自文档。.


如果直接继承UIView,则此方法
的实现无需调用super。但是,如果要继承
不同的视图类,则应在
实现中的某个时刻调用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."

链接到文档

我假设您的超类是UIView。因此,即使不调用[super drawrect:rect]也可以。因为drawRect:因为UIView什么都不做。因此,在这种情况下,是否调用[super drawrect:rect]都没有区别。

I am assuming, your super class is UIView. Hence it works even if [super drawrect:rect] is not called. Because the drawRect: because UIView does nothing. So it makes no difference in this case whether or not you call [super drawrect:rect].

这篇关于为什么drawRect:在不调用[super drawrect:rect]的情况下工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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