【发布时间】:2012-10-31 23:46:38
【问题描述】:
我有一个非常简单的项目,其中涉及使用 drawRect 绘制形状,但我不相信我的 drawRect 函数实际上被调用了。这是代码。 (其他一切都只是“单一视图应用程序”的默认设置。)
-(void)drawRect:(CGRect)aRect{
NSLog(@"testing...");
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 120);
CGContextAddLineToPoint(context, 20, 100);
CGContextClosePath(context);
[[UIColor greenColor] setFill];
[[UIColor redColor] setStroke];
CGContextDrawPath(context,kCGPathFillStroke);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setNeedsDisplay];
}
任何建议都会很棒——感谢阅读!
【问题讨论】:
-
看起来您已将
drawRect:放在视图控制器类中。视图控制器不实现drawRect:,UIView的子类实现。 -
是的,您只需在
self上致电setNeedsDisplay。 -
好的,谢谢!仍在学习如何做到这一点,所以会坚持下去。
-
@Paul.s:该评论应该是一个答案。
标签: objective-c drawrect setneedsdisplay