我有一个简单的测试应用程序。该应用程序由应用程序委托,视图控制器和视图组成。应用程序委托只是启动视图控制器并将其视图添加到窗口。视图控制器依次在启动时加载视图,不执行任何其他操作。视图如下所示:
@implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
static NSDate* prevDate = nil;
NSDate* now = [NSDate date];
NSString* timeString = @"";
if (prevDate) {
NSTimeInterval dt = [now timeIntervalSinceDate:prevDate];
timeString = [NSString stringWithFormat:@"%d seconds", (int)dt];
}
prevDate = now;
NSLog (@"drawRect %@ %@", NSStringFromCGRect(rect), timeString);
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGRect rect = CGRectMake(point.x, point.y, 40, 40);
[self setNeedsDisplayInRect:rect];
}
@end
经过一些接触,这是我的输出。我不明白的是 - 为什么drawRect有时会在整个屏幕上调用,而不仅仅是小矩形。
每次drawRect调用都会在触摸后立即进行。这是预期的。但为什么有些调用指定了大矩形呢?
2011-12-13 12:46:15.004 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}}
2011-12-13 12:46:20.197 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 5 seconds
2011-12-13 12:46:23.235 DrawRectTest[1451:707] drawRect {{508, 475}, {40, 40}} 3 seconds
2011-12-13 12:46:27.671 DrawRectTest[1451:707] drawRect {{761, 484}, {40, 40}} 4 seconds
2011-12-13 12:46:36.394 DrawRectTest[1451:707] drawRect {{433, 254}, {40, 40}} 8 seconds
2011-12-13 12:46:51.190 DrawRectTest[1451:707] drawRect {{597, 270}, {40, 40}} 14 seconds
2011-12-13 12:47:04.979 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 13 seconds
2011-12-13 12:47:09.148 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 4 seconds
2011-12-13 12:47:14.314 DrawRectTest[1451:707] drawRect {{414, 480}, {40, 40}} 5 seconds
2011-12-13 12:47:31.343 DrawRectTest[1451:707] drawRect {{551, 503}, {40, 40}} 17 seconds
2011-12-13 12:47:33.639 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 2 seconds
2011-12-13 12:47:35.554 DrawRectTest[1451:707] drawRect {{521, 519}, {40, 40}} 1 seconds
2011-12-13 12:47:41.325 DrawRectTest[1451:707] drawRect {{366, 586}, {40, 40}} 5 seconds
2011-12-13 12:47:50.751 DrawRectTest[1451:707] drawRect {{535, 474}, {40, 40}} 9 seconds
2011-12-13 12:48:01.891 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 11 seconds
2011-12-13 12:49:24.600 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 82 seconds
2011-12-13 12:49:29.514 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 4 seconds
2011-12-13 12:49:30.774 DrawRectTest[1451:707] drawRect {{725, 372}, {40, 40}} 1 seconds
2011-12-13 12:50:04.435 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 33 seconds
2011-12-13 12:50:07.469 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 3 seconds
2011-12-13 12:50:09.417 DrawRectTest[1451:707] drawRect {{824, 471}, {40, 40}} 1 seconds
2011-12-13 12:51:04.550 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 55 seconds
2011-12-13 12:51:06.071 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 1 seconds
2011-12-13 12:51:07.809 DrawRectTest[1451:707] drawRect {{453, 557}, {40, 40}} 1 seconds
2011-12-13 12:51:11.776 DrawRectTest[1451:707] drawRect {{510, 370}, {40, 40}} 3 seconds
如果重要,我问的原因是我正在努力提高大型项目的绩效。缓慢的原因之一是那里有类似的drawRect调用。
答案 0 :(得分:1)
UIViews不保证在更新或在随后的drawRect中在该视图中进行任何更多绘图时保存或重用其先前绘制的任何图形内容(之前的绘图已经以只写方式发送到GPU)。因此,任何绘图,甚至是一小部分,都可能需要重新绘制整个视图以重新创建该视图的内容。
如果你只想绘制一个小区域,你需要绘制一个UIView以外的东西,可能是你的应用已分配的位图上下文。
答案 1 :(得分:0)
我会猜测并假设您的三角调用代码不仅仅是触摸消息。也许还有其他系统消息被传递,你的听众也会对它们做出反应。我没有看到你如何处理这些问题,我绝不是Max专家,但这首先是我想到的。