在我的代码中,我想“动画”绘制一条线的延迟,因此在向视图添加新线后,我调用 setNeedsDisplay - 一次可以正常工作。
在 drawRect 方法中,我绘制了线条并调用线条的方法来增加线条长度l。现在我想再次调用 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。