Chr*_*ler 2 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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
_lines是一个带有GameLine对象(非原子,保留)属性的NSMutableArray.
这是预料之中的.
调用setNeedsDisplay时,将视图标记为需要重绘.好.系统得到它.
它将在下次应用程序的主循环运行时完成.
如果你真的想刷新视图现在调用:
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
Run Code Online (Sandbox Code Playgroud)
就在之后setNeedsDisplay.
确实,苹果文档说明(强调我的):
当视图的实际内容发生变化时,您有责任通知系统您的视图需要重新绘制.您可以通过调用视图的视图的setNeedsDisplay或setNeedsDisplayInRect:方法来完成此操作.这些方法让系统知道它应该在下一个绘图周期中更新视图.因为它会等到下一个绘图周期来更新视图,所以您可以在多个视图上调用这些方法来同时更新它们.
另外,请看这些SO问题:
| 归档时间: |
|
| 查看次数: |
1265 次 |
| 最近记录: |