在我的代码,我想“动画”画线的延迟,因此添加一个新行到视图后,我打电话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(非原子,保留)财产。
我添加了一个“NSLog(@”Count:%d“,[_l ines count]);“到for循环之前的drawRect,它会在添加一行之前出现几次 - 然后再次一次,然后再次没有次数。 – 2013-04-04 20:32:59
@ Christian'fuzi'Orgler一旦您调用了'setNeedsDisplay',系统将不会考虑任何其他呼叫,直到重新绘制视图(*即直到您的应用程序主循环运行*)。这是因为你已经明确了该视图应该重绘**下一次模态循环运行**。如果您希望每次调用'setNeedsDisplay'时重新绘制视图,请使用我的答案中提供的代码。 – Jean 2013-04-04 20:35:54
注意:在'drawRect:'中调用'setNeedsDisplay'会导致循环调用,导致性能很差。 – Jean 2013-04-04 20:41:34