【问题标题】:Objective C: Implementing drawRect目标 C:实现 drawRect
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多