我正在使用CoreGraphics進行渲染的繪畫應用程序。我的問題是,爲了提高性能,我試圖將更新限制爲僅改變視圖的某些部分。爲此,我使用setNeedsDisplayInRect :,但有時視圖更新其整個內容,導致口吃。這在第三代iPad上尤其明顯,但它也在模擬器和iPad2中發生的程度較低。我正試圖消除這種行爲。setNeedsDisplayInRect:導致整個視圖更新
爲了展示這個問題,我使用Xcode中的模板創建了一個簡單的「單視圖」項目。創建了一個自定義的UIView子類,我將其設置爲xib文件中的視圖控制器視圖。
已將此添加的UIViewController:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Asks that the view refreshes a smal rectangle
[self.view setNeedsDisplayInRect:CGRectMake(10, 10, 20, 20)];
}
已將此添加到MyView的(我的自定義視圖):
- (void)drawRect:(CGRect)rect
{
// Just log what is being updated
NSLog(@"%@", NSStringFromCGRect(rect));
}
就是這樣。如果我在第三十臺iPad(實際設備)上運行該應用程序,則日誌會顯示該視圖時常(如非常頻繁)重新繪製其本身的內容。這太令人沮喪了,我不知道
更新:這裏有一些日誌。它絕對顯示有時更新的全景。我試圖使它完全停止,但找不出如何...
2012-05-04 08:34:01.851 TestUpdateArea[45745:707] {{0, 0}, {320, 460}}
2012-05-04 08:34:30.184 TestUpdateArea[45745:707] {{0, 0}, {320, 460}}
2012-05-04 08:34:30.197 TestUpdateArea[45745:707] {{0, 0}, {320, 460}}
2012-05-04 08:34:30.215 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.226 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.242 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.258 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.274 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.290 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.306 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.322 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.338 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.354 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.371 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.387 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.403 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.419 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:30.439 TestUpdateArea[45745:707] {{0, 0}, {320, 460}}
2012-05-04 08:34:30.457 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
另外,如果我停止10s以上左右,恢復動人,它肯定不會很一致:
2012-05-04 08:34:33.305 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:34:33.321 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
2012-05-04 08:35:00.202 TestUpdateArea[45745:707] {{0, 0}, {320, 460}}
2012-05-04 08:35:00.221 TestUpdateArea[45745:707] {{0, 0}, {320, 460}}
2012-05-04 08:35:00.234 TestUpdateArea[45745:707] {{0, 0}, {320, 460}}
2012-05-04 08:35:00.251 TestUpdateArea[45745:707] {{10, 10}, {20, 20}}
在我的情況,當drawRect在setNeedsDisplayInRect之後調用**第一次**時,rect是滿邊界(不要問我爲什麼),任何下一次調用它就是我在setNeedsDisplayInRect中指定爲參數的rect。您是否測試觸摸移動不止一次? –
它直接發生在設備上。但是,如果您移動手指並等待約10秒或15秒,然後再次觸摸並移動,則可以在模擬器上進行操作。在iPad3設備上,即使在手指移動時也會發生。 Sooo討厭:) – mprivat
我張貼@這一段時間回來(http://stackoverflow.com/questions/9299018/setneedsdisplayinrect-bug-in-ios5)。這似乎只是在第一個電話。我從來沒有找到一個原因/修復,只是繼續前進,因爲我不需要這樣的性能優化級別。你可以測試rect ==視圖大小而不是繪製?也許不是一種選擇。 –