2011-12-13 19 views
3

我有一個簡單的測試應用程序。該應用程序由應用程序委託,視圖控制器和視圖組成。應用程序委託只是啓動視圖控制器並將其視圖添加到窗口。視圖控制器反過來在啓動時加載視圖,除此之外別無其他。該視圖看起來像這樣:iOS - drawRect有時會在意外的大矩形上調用

@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調用。

回答

1

UIView不保證在更新或在隨後的drawRect中執行任何更多的繪圖時(先前的繪圖已經發送至只寫GPU),保存或重新使用它們之前繪製的任何圖形內容。因此,任何繪圖(即使是很小的一部分)都可能需要重新繪製整個視圖以重新創建該視圖的內容。

如果你想繪製到一個小區域,你需要繪製到UIView以外的東西,也許是繪製到你的應用程序已經分配給的實例的位圖上下文中。

0

我打算大膽猜測,並假設你的三角形調用代碼不僅僅是觸摸消息的反應。也許還有其他系統消息被傳遞,你的監聽器也在響應它們。我沒有看到關於你如何處理這個問題的具體細節,我絕不是Max的專家,但這是我首先想到的。

+0

三角形調用代碼是什麼意思? – 2011-12-13 19:50:25

相關問題