目标C:实现drawRect

Rog*_*are 1 objective-c drawrect setneedsdisplay

我有一个非常简单的项目,涉及使用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];
}
Run Code Online (Sandbox Code Playgroud)

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

Pau*_*l.s 6

看起来你已经把它放在drawRect:一个UIViewController子类中.UIViewController没有实现drawRect:,UIViewdo的子类.

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