0

这是我的问题。我计划在设置时在视图内绘制 999 个圆圈,这在 drawRect 中成功完成。之后,我计划在用户触摸时编辑每个圆圈的颜色(我正在使用视图控制器中的点击手势进行处理)。问题是,当我调用 [self.pegView setNeedsDisplay]; drawRect 被调用并转到 drawRect 中 if 的 else 部分(它应该),但是所有其他珠子都被删除了,我只看到 1 个珠子而不是 999 个珠子。

下面是我的代码。

- (void)drawRect:(CGRect)rect
{

    if(!self.isEditingHappening) // For the initial setup
    {
        CGRect beadRect = CGRectMake(BEAD_ORIGIN_X, BEAD_ORIGIN_Y, BEAD_VIEW_SIZE, BEAD_VIEW_SIZE);
        self.ctx = UIGraphicsGetCurrentContext();

        for(int i = 0 ; i < 999 ; i++)
        {
            // To set up the bead view for each column
            if (i !=0 && i % 37 !=0)
            {
                beadRect.origin = CGPointMake((beadRect.origin.x + BEAD_VIEW_SIZE), beadRect.origin.y);
            }

            // To set up bead view for each row
            if (i !=0 && i %37 ==0 )
            {
                beadRect.origin.y += BEAD_VIEW_SIZE;
                beadRect.origin = CGPointMake(BEAD_ORIGIN_X, beadRect.origin.y);
            }

            BeadPattern *pattern = [self.beadPatternsArray objectAtIndex:(i/37)];

            int beadType=[[pattern.eachRowPattern objectAtIndex:i%37] intValue];
            BeadColor *color=[self.beadColorsArray objectAtIndex:beadType];


            CGPoint center;
            center.x = beadRect.origin.x + beadRect.size.width / 2.0;
            center.y = beadRect.origin.y + beadRect.size.height / 2.0;

            CGRect newInnerRect;
            newInnerRect.size.width =  beadRect.size.width * 0.6;
            newInnerRect.size.height = beadRect.size.height * 0.6;
            newInnerRect.origin.x = center.x - newInnerRect.size.width/2;
            newInnerRect.origin.y = center.y - newInnerRect.size.height/2;



            [[UIColor whiteColor] set];
            UIRectFill(beadRect);

            // Adds the outer circle
            CGContextAddEllipseInRect(self.ctx, beadRect);

            // Fill the outer circle with any color
            CGContextSetRGBFillColor(self.ctx, color.redValue, color.greenValue, color.blueValue, 1);

            CGContextFillEllipseInRect(self.ctx, beadRect);

            //Add inner circle
            CGContextAddEllipseInRect(self.ctx, newInnerRect);


            //Fill inner circle with white color
            CGContextSetRGBFillColor(self.ctx, 1, 1, 1, 0.4);
            CGContextFillEllipseInRect (self.ctx, newInnerRect);

        }
    }
    else    // When editing is happening
    {
        NSLog(@"The redrawing rect is %@", NSStringFromCGRect(rect));
        CGContextSaveGState(self.ctx);
        CGRect beadRect;
        beadRect.size = CGSizeMake(BEAD_VIEW_SIZE, BEAD_VIEW_SIZE);
        beadRect.origin = CGPointMake(self.columnToBeUpdated * BEAD_VIEW_SIZE, self.rowToBeUpdated * BEAD_VIEW_SIZE);

        CGPoint center;
        center.x = beadRect.origin.x + beadRect.size.width / 2.0;
        center.y = beadRect.origin.y + beadRect.size.height / 2.0;

        CGRect newInnerRect;
        newInnerRect.size.width =  beadRect.size.width * 0.6;
        newInnerRect.size.height = beadRect.size.height * 0.6;
        newInnerRect.origin.x = center.x - newInnerRect.size.width/2;
        newInnerRect.origin.y = center.y - newInnerRect.size.height/2;

        CGContextRestoreGState(self.ctx);

        [[UIColor whiteColor] set];
        UIRectFill(beadRect);

        // Adds the outer circle
        CGContextAddEllipseInRect(self.ctx, beadRect);

        // Fill the outer circle with any color
        CGContextSetRGBFillColor(self.ctx, self.colorToBeShownWhileEdited.redValue, self.colorToBeShownWhileEdited.greenValue, self.colorToBeShownWhileEdited.blueValue, 1);

         CGContextFillEllipseInRect(self.ctx, beadRect);

        //Add inner circle
        CGContextAddEllipseInRect(self.ctx, newInnerRect);

        //Fill inner circle with white color
        CGContextSetRGBFillColor(self.ctx, 1, 1, 1, 0.4);
        CGContextFillEllipseInRect (self.ctx, newInnerRect);
     }
  }

我打算做的是,保留所有 999 个珠子,但只编辑用户触摸过的一个珠子的颜色。我也尝试使用 [setNeedsDisplayInRect],但没有正确传递 rect。请在这里帮助我。

4

1 回答 1

0

如果我没记错的话,它会清除画布上的其他项目——所以你的初始设置每次都需要运行。

或者,您可以为每个珠子创建单独的视图,这样您就可以专门关注那个珠子。每个珠子都可以包含自己的逻辑。

于 2012-09-10T18:51:18.290 回答