我正在尝试setNeedsDisplayInRect:
在 iOS5 中使用来优化一些绘图代码。设置很简单:我有一个 CGRect '热点'数组,用作按钮。当检测到触摸时,我找到它发生的 CGRect 并setNeedsDisplayInRect:
以该矩形作为参数调用视图。所有的 CGRects 对视图都是有效的——它使用它们来进行初始绘图并且正确显示。
我所看到的(如下面的控制台转储所示)是第一次调用setNeedsDisplayInRect:
将视图框架作为矩形传递,而不是我指定的矩形。随后的调用是正确的。
谁能确认这是一个错误或看到我在这里做错了什么?所有代码都在下面。 - 谢谢!
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
}