setNeedsDisplay只被调用一次

时间:2013-04-04 20:22:38

标签: iphone objective-c drawrect method-call setneedsdisplay

在我的代码中,我希望“动画”绘制一条线的延迟,所以在向视图中添加一个新行后,我调用setNeedsDisplay - 它工作正常一次。

在drawRect方法中,我绘制线并调用线的方法来增加line-lengthl。现在我想再次调用setNeedsDisplay来重绘这一行 - 所以它是一个“成长”的动画..

但它只调用setNeedsDisplay一次&再也不会,除了我添加另一行。 我也尝试在这个类中调用一个方法,调用setNeedsDisplay,以确保你不能在drawRect中调用它。

- (void)drawRect:(CGRect)rect {

    for(GameLine *line in _lines) {

        if(line.done) {
            CGContextRef c = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(c, 5.0f);
            CGContextSetStrokeColor(c, lineColor);

            CGContextBeginPath(c);
            CGContextMoveToPoint(c, line.startPos.x, line.startPos.y);
            CGContextAddLineToPoint(c, line.endPos.x, line.endPos.y);
            CGContextStrokePath(c);
        }else {
            CGContextRef c = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(c, 5.0f);
            CGContextSetStrokeColor(c, delayColor);

            CGContextBeginPath(c);
            CGContextMoveToPoint(c, line.delayStartPos.x, line.delayStartPos.y);
            CGContextAddLineToPoint(c, line.delayEndPos.x, line.delayEndPos.y);
            CGContextStrokePath(c);

            [line incrementDelayLine];
            [self setNeedsDisplay];
        }
    }
}

_lines 是带有GameLine对象(非原子,保留)属性的NSMutableArray。

2 个答案:

答案 0 :(得分:5)

预期。

调用setNeedsDisplay时,将视图标记为需要重绘。好。系统得到它。
它将在您的应用程序的主循环下次运行时完成。

如果您确实要刷新视图现在,请致电:

[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];

setNeedsDisplay之后。

确实,apple documentation州(强调我的):

  

当视图的实际内容发生变化时,它就是您的   责任通知系统您的视图需要   重绘。您可以通过调用视图的 setNeedsDisplay 或来执行此操作   setNeedsDisplayInRect:视图的方法。 这些方法让   系统知道它应该在下一次绘图期间更新视图   循环。因为等待下一个绘图周期来更新   在view中,您可以在多个视图上调用这些方法来更新它们   同时。

另外,请参阅以下SO问题:

答案 1 :(得分:1)

如果您需要动画 - 启动计时器,一旦它被触发 - 调整您想要的任何行参数并调用setNeedsDisplay

相关问题