2012-02-15 48 views
2

我試圖在iOS5中使用setNeedsDisplayInRect:來優化一些繪圖代碼。設置很簡單:我有一個CGRect'熱點'數組,作爲按鈕。當檢測到觸摸時,我找到它發生的CGRect,並在該視圖上用該rect作爲參數調用setNeedsDisplayInRect:。所有的CGRects對於視圖都是有效的 - 它使用它們來完成初始繪製並且正確顯示。set iOS5中的setNeedsDisplayInRect錯誤?

我所看到的(如下面的控制檯轉儲所示)是第一個調用setNeedsDisplayInRect:的視圖幀作爲rect傳遞,而不是我指定的矩形。隨後的通話是正確的。

任何人都可以證實這是一個錯誤,或看到我在這裏做錯了什麼嗎?所有代碼如下。 - 謝謝!

WRONG -> drawRect with rect 0.000000 0.000000 70.000000 660.000000 
drawRect with rect 15.000000 260.000000 40.000000 40.000000 
drawRect with rect 15.000000 310.000000 40.000000 40.000000 
drawRect with rect 15.000000 360.000000 40.000000 40.000000 
drawRect with rect 15.000000 410.000000 40.000000 40.000000 
drawRect with rect 15.000000 460.000000 40.000000 40.000000 
drawRect with rect 15.000000 510.000000 40.000000 40.000000 
drawRect with rect 15.000000 410.000000 40.000000 40.000000 
drawRect with rect 15.000000 310.000000 40.000000 40.000000 
drawRect with rect 15.000000 260.000000 40.000000 40.000000 
drawRect with rect 15.000000 110.000000 40.000000 40.000000 
drawRect with rect 15.000000 610.000000 40.000000 40.000000 
drawRect with rect 15.000000 510.000000 40.000000 40.000000 



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *touch = [touches anyObject]; 
CGPoint point = [touch locationInView:self.view]; 

    CGRect rect; 
    int cnt = self.barNoteArray.count; 
    for (int i = 0; i < cnt; i++) { 
     rect = [[self.barNoteArray objectAtIndex:i] cellRect]; 
     if (CGRectContainsPoint(rect, point)) { 

      self.bar.highlightIndex = 1; 
      [self.bar setNeedsDisplayInRect:rect]; 

      break; 
     } 
    } 
} 



- (void)drawRect:(CGRect)rect 
{ 
if (highlightIndex){ 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor); 
    CGContextAddRect(context, rect); 
    CGContextFillPath(context); 

    highlightIndex = 0; 

    printf("drawRect with rect %f %f %f %f \n", rect.origin.x, 
      rect.origin.y, 
      rect.size.width, 
      rect.size.height); 
} else { 
    // other drawing code 


} 

回答

2

我也有這個問題。我已經把範圍縮小到以下情況:

看來,當我從一個UIView的轉移到第二個這種情況正在發生。在我的情況下,用戶在一個UIView中選擇一個繪畫工具,然後在底層的UIView(畫布)上繪圖。

似乎初始行程從你放什麼東西在裏面全視圖大小變化的drawRect收到的矩形。一旦繪製了一個或兩個drawRects,它就不會改變你放在那裏的東西。

我可以證明,這是一個setNeedsDisplayInRect,因爲如果我評論違規線路輸出的drawRect不再被調用。在調用之前顯示矩形,然後更新合適的子矩形,drawRect中的矩形顯示不同的矩形。

同樣,這隻發生在從一個UIView到另一個(看起來)的初始移動中。