【发布时间】:2010-07-03 02:14:44
【问题描述】:
我面前有两个 Quartz iPhone 应用程序。在它们中的每一个中,对 setNeedsDisplay 的调用都会导致视图自身重绘。但是有一个重要的区别。在一种情况下(Mark/Lamarche 书籍“Beginning iPhone development”中的“Quartz Fun”应用程序),每次开始时视图都是空白的。在另一种情况下(我正在开发的应用程序),视图从以前的任何内容开始,并在其上添加新图形。
我不明白这是为什么。谁能帮我解答一下?
谢谢。
编辑#2:我仍然不明白这里发生了什么。但是我发现我可以通过调用来清除我的视图
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.frame);
编辑#3:显示缩短的代码:
根据评论者的建议,我已经编辑了我的应用程序,因此只需很少的代码就会出现问题。 [现在问题的形式有点不同,如下所述。]
应用委托:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
DiceView *dv = [[DiceView alloc]initWithFrame: window.frame];
[window addSubview:dv];
[dv release];
[window makeKeyAndVisible];
return YES;
}
骰子视图:
- (void)drawRect:(CGRect)rect {
static int nDrawrectCalls = 0;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, (nDrawrectCalls%5==0?[UIColor redColor]:[UIColor greenColor]).CGColor);
CGContextMoveToPoint(context, 10, 30+10*nDrawrectCalls);
CGContextAddLineToPoint(context, 300, 30+10*nDrawrectCalls);
CGContextStrokePath(context);
nDrawrectCalls++;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setNeedsDisplay];
}
其他一切都只是默认的存根方法。
现在来看看区别。它现在似乎开始使用两次触摸之前屏幕上的任何内容进行绘制。换句话说,在 touch #2 之后,我看到了最初的行,加上 touch #2 的行——但不是 touch #1 的行。在 touch #3 之后,我看到 touch #1 和 #3 的线条。在 touch #4 之后,我看到了初始行以及 touch #2 和 #4 的行。以此类推。
【问题讨论】:
-
贴出“在其上添加新图形”的代码