iOS5中的setNeedsDisplayInRect错误? [英] setNeedsDisplayInRect bug in iOS5?

查看:95
本文介绍了iOS5中的setNeedsDisplayInRect错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iOS5中使用 setNeedsDisplayInRect:来优化一些绘图代码。设置很简单:我有一组CGRect'热点'作为按钮。当检测到触摸时,我找到它发生的CGRect并在该视图上以该矩形作为参数调用 setNeedsDisplayInRect:。所有的CGRect都对视图有效 - 它使用它们进行初始绘制并且输出正确。

I'm trying to use setNeedsDisplayInRect: in iOS5 to optimize some drawing code. The setup is simple: I have an array of CGRect 'hotspots' that function as buttons. When a touch is detected I find the CGRect it occurred in and call setNeedsDisplayInRect: on the view with that rect as a param. All the CGRects are valid for the view - it uses them to do it's initial drawing and that comes out correctly.

我所看到的(如下面的控制台转储所示)是第一次调用 setNeedsDisplayInRect:将视图框作为rect传递,而不是我指定的矩形。后续调用是正确的。

What I am seeing (as the Console dump below shows) is that the first call to setNeedsDisplayInRect: passes the view frame as rect, not the rect I specified. Subsequent calls are correct.

任何人都可以确认这是一个错误,或者看到我在这里做错了吗?所有代码都在下面。 - 谢谢!

Can anyone confirm this as a bug or see that I am doing something incorrectly here? All the code is below. -- Thanks!

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


}


推荐答案

我也有这个问题。我把它缩小到以下情况:

I am having this problem as well. I have narrowed it down to the following situation:

当我从一个UIView转移到第二个UIView时,似乎发生了这种情况。在我的例子中,用户在一个UIView中选择一个绘画工具,然后在底层UIView(画布)上绘图。

It seems that this is happening when I shift from one UIView to a second one. In my case, the user it selecting a painting tool in one UIView, then drawing on a underlaying UIView (the canvas).

似乎初始笔划改变了收到的矩形在drawRect中,从你放在那里到完整的视图大小。一旦绘制了一个或两个drawRects,它就不会改变你放在那里的东西。

It seems the initial stroke changes the rect received in drawRect from what you put in there to the full view size. Once those one or two drawRects have been drawn, it does not change what you put in there.

我可以证明它是一个setNeedsDisplayInRect,因为如果我评论有问题的行不再调用drawRect。在调用之前的rect显示然后更新正确的子矩形,drawRect中的rect显示不同的矩形。

I can prove that it is a setNeedsDisplayInRect because if I comment the offending line out drawRect no longer is called. The rect just before the call shows then proper sub rectangle to update, the rect in drawRect shows a different rectangle.

再次,这只发生在从一个初始移动UIView到另一个(似乎)。

Again, this is only happening on the initial move from one UIView to another (it seems).

这篇关于iOS5中的setNeedsDisplayInRect错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆