在我的代碼,我想「動畫」畫線的延遲,因此添加一個新行到視圖後,我打電話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