1

我有一个非常简单的项目,涉及使用 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];
}

任何建议都会很棒——感谢阅读!

4

1 回答 1

6

看起来您已将 放置drawRect:UIViewController子类中。UIViewController的不实现drawRect:UIView做的子类。

您需要UIView使用您的实现创建一个自定义子类,drawRect:然后将其作为视图控制器属性的子级self.view或作为根视图本身添加到您的视图层次结构中。

于 2012-11-01T09:21:08.137 回答