wak*_*eup 10 core-graphics ios
我正在覆盖drawRect:在我的一个视图中,它甚至可以在不调用[super drawrect:rect]的情况下工作.这是如何运作的?
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeDestinationOver);
[[UIColor clearColor] setFill];
UIRectFill(CGRectMake(0,0,100,100));
}
Run Code Online (Sandbox Code Playgroud)
基本上,它的工作原理是因为,设置了图形上下文进行绘制机械等,并没有住在UIView实施-drawRect:(这是空的).
那个机器确实住在某个地方,当然,让我们假装每个UIView都有一些像这样的内部方法 - 方法,功能和属性的名称是为了保护无辜而发明的:
- (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();
}
Run Code Online (Sandbox Code Playgroud)
这是一种更简洁的构建框架的方法,而不是依赖子类化程序在公共覆盖中做正确的事情,在这种情况下,错误会导致非常令人费解的不良行为.(这根本不可能,因为平局可能需要设置和拆卸步骤.)