为什么必须从 drawRect 方法中调用 Quartz 2D 图形上下文函数?
因为如果我从除 drawRect 之外的任何地方调用 CGGraphics 上下文函数,我会收到如下消息:
<Error>: CGContextFillRects: invalid context
<Error>: CGContextSetFillColorWithColor: invalid context
实际上,在 UIView 的一个子类中,我确实在一个名为 Render 的方法中设置了一个图形上下文。打赌当 Render 被调用时,我得到了上述错误:
- (void)Render {
CGContextRef g = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(g, [UIColor blueColor].CGColor);
CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
[@"It works!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
NSLog(@"gsTest Render");
}