Why does drawRect: work without calling [super drawrect:rect]?

这一生的挚爱 提交于 2019-12-20 10:38:14

问题


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));
}

回答1:


From the documentation:

"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"

Also from the documentation..

"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."

Link to the documentation

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].




回答2:


Basically, it works because the machinery that sets up the graphics context for drawing, etc, doesn't live in the UIView implementation of -drawRect: (which is empty).

That machinery does live somewhere, of course, let's pretend in every UIView there's some internal method like this-- names of methods, functions, and properties are invented to protect the innocent:

- (void)_drawRectInternal:(CGRect)rect
{
    // Setup for drawing this view.
    _UIGraphicsContextPushTransform(self._computedTransform);

    // Call this object's implementation of draw (may be empty, or overridden).
    [self drawRect:rect];

    // draw our subviews: 
    for (UIView * subview in [self subviews]) {
        [subview _drawRectInternal:rect];
    }

    // Teardown for this view.
    _UIGraphicsContextPopTransform();
}

This is a cleaner way of building the frameworks than relying on a subclasser to do the right thing in a common override, where getting it wrong would have result in very puzzling bad behavior. (And this wouldn't really be possible at all since the draw might need a setup and a teardown step.)




回答3:


I'm sure subview doesn't work without calling [super drawrect:rect].If you need to keep the superview's drawing ,you must add [super drawrect:rect] in the subview's drawrect:rect,or you'll replace the drawing.




回答4:


Thanks.. There is no need to call super, because the superclass here is UIView, whose drawRect: does nothing.

In general, we call setNeedDisplay if we want recall drawRect for drawing.



来源:https://stackoverflow.com/questions/26479524/why-does-drawrect-work-without-calling-super-drawrectrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!