我正在尝试使用iOS5中的setNeedsDisplayInRect:来优化一些绘图代码。设置很简单:我有一个CGRect“热点”数组,它们的功能是按钮。当检测到触摸时,我会找到它所在的CGRect,并使用该rect作为参数在视图上调用setNeedsDisplayInRect:。所有的CGRects对于视图都是有效的-它使用它们来完成它的初始绘制,并正确地显示出来。
我所看到的(如下面的控制台转储所示)是,对setNeedsDisplayInRect:的第一个调用将视图帧作为rect传递,而不是我指定的rect。后续调用是正确的。
有没有人可以确认这是一个bug,或者看到我在这里做了一些不正确的事情?所有代码都在下面。--谢谢!
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
}发布于 2012-04-01 08:57:58
我也有这个问题。我已经将其范围缩小到以下情况:
当我从一个UIView切换到另一个时,似乎发生了这种情况。在我的例子中,用户选择一个UIView中的绘画工具,然后在底层UIView (画布)上绘图。
似乎最初的笔划将drawRect中接收到的矩形从您放入的内容更改为完整的视图大小。一旦绘制了这一两个drawRects,就不会更改您放入其中的内容。
我可以证明它是一个setNeedsDisplayInRect,因为如果我注释掉有问题的行,drawRect将不再被调用。在调用之前的rect显示要更新的正确的子矩形,drawRect中的rect显示一个不同的矩形。
同样,这只发生在从一个UIView到另一个的初始迁移(看起来是这样)。
https://stackoverflow.com/questions/9299018
复制相似问题