我試圖在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
}