setNeedsDisplayInRect 问题(quartz2d,iPhone)

发布于 2024-08-18 06:58:47 字数 2738 浏览 0 评论 0原文

我编写了一个函数,每 2 秒在随机位置绘制 x 个正方形。一个正方形不能在同一位置画两次。一旦应用程序完成一个循环,网格就会被填充。现在,当绘制的方块数量 <= 8 时,这可以正常工作,但是当该数量更高时,方块会随机绘制到与前一个方块相同的位置。

我没有将正方形绘制到随机位置,而是尝试从上到下绘制它们,这非常有效。

我已经对此进行了几天的调试,我相信问题出在 setNeedsDisplayInRect 上。

希望有人能帮忙。抱歉,如果我胡言乱语了,已经晚了,我的头已经烂了,哈哈:)

下面是代码....

- (void)drawRect:(CGRect)rect {
int count = [writeSquares count];

CGContextRef context = UIGraphicsGetCurrentContext();

int radius = 4;

for (int i = 0; i <count; i++)
{
    Square *square = [writeSquares objectAtIndex: i];

    CGContextSetLineWidth(context, 1);
    //CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetFillColorWithColor(context, [square color].CGColor);
    CGContextMoveToPoint(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMinY([square squareRect]));
    CGContextAddArc(context, CGRectGetMaxX([square squareRect]) - radius, CGRectGetMinY([square squareRect]) + radius, radius, 3 * M_PI / 2, 0, 0);
    CGContextAddArc(context, CGRectGetMaxX([square squareRect]) - radius, CGRectGetMaxY([square squareRect]) - radius, radius, 0, M_PI / 2, 0);
    CGContextAddArc(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMaxY([square squareRect]) - radius, radius, M_PI / 2, M_PI, 0);
    CGContextAddArc(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMinY([square squareRect]) + radius, radius, M_PI, 3 * M_PI / 2, 0);  
    CGContextClosePath(context);
    CGContextFillPath(context);

    //NSLog(@"x = %f x = %f", CGRectGetMaxX([square squareRect]), CGRectGetMinX([square squareRect]));
}

//NSLog(@"Count %i", [writeSquares count]);

[writeSquares removeAllObjects];

}

-(void)drawInitializer:(int) counter{

if(extra != 0) {
    [self setNeedsDisplay];
    [grid removeAllObjects];
    [self calculateGrid];
    counter = counter + extra;
    clear = YES;
    extra = 0;
}

for(int i=0; i<counter; i++) {
    int count = [grid count];
    NSLog(@"counter %i", count);
    if(count != 144) {
        srandom(time(NULL));
        int r = random() % [coordinates count];

        Coordinate *coordinate = [coordinates objectAtIndex:r];


        Square *square = [Square new];
        [square setSquareRect: CGRectMake ([coordinate x] * 25, [coordinate y] * 25, 20, 20)];
        [square setColor: [UIColor randomColor]];
        [writeSquares addObject:square];
        [grid addObject:square];
        [square release];

        [self setNeedsDisplayInRect:CGRectMake ([coordinate x] * 25, [coordinate y] * 25, 20, 20)];

        [coordinates removeObjectAtIndex:r];
        [coordinate release];
    }
    else {
        extra++;
    }
}

//[newlock release];

}

I have written a function that draws x number of squares in a random position every 2 seconds. A square cannot be drawn in the same position twice. Once the app has completed a cycle the grid will be filled in. Now, this works fine when the number of squares drawn is <=8, however when this is higher a square is randomly drawn into the same position as a previous square.

Instead of drawing a square into a random position I have tried drawing them from top-bottom and this works perfectly.

I've been debugging this for a couple of days and I believe the problem lies with setNeedsDisplayInRect.

Hope someone can help. Sorry if I have rambled, its getting late and my head is mashed, lol :)

Below is the code....

- (void)drawRect:(CGRect)rect {
int count = [writeSquares count];

CGContextRef context = UIGraphicsGetCurrentContext();

int radius = 4;

for (int i = 0; i <count; i++)
{
    Square *square = [writeSquares objectAtIndex: i];

    CGContextSetLineWidth(context, 1);
    //CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetFillColorWithColor(context, [square color].CGColor);
    CGContextMoveToPoint(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMinY([square squareRect]));
    CGContextAddArc(context, CGRectGetMaxX([square squareRect]) - radius, CGRectGetMinY([square squareRect]) + radius, radius, 3 * M_PI / 2, 0, 0);
    CGContextAddArc(context, CGRectGetMaxX([square squareRect]) - radius, CGRectGetMaxY([square squareRect]) - radius, radius, 0, M_PI / 2, 0);
    CGContextAddArc(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMaxY([square squareRect]) - radius, radius, M_PI / 2, M_PI, 0);
    CGContextAddArc(context, CGRectGetMinX([square squareRect]) + radius, CGRectGetMinY([square squareRect]) + radius, radius, M_PI, 3 * M_PI / 2, 0);  
    CGContextClosePath(context);
    CGContextFillPath(context);

    //NSLog(@"x = %f x = %f", CGRectGetMaxX([square squareRect]), CGRectGetMinX([square squareRect]));
}

//NSLog(@"Count %i", [writeSquares count]);

[writeSquares removeAllObjects];

}

-(void)drawInitializer:(int) counter{

if(extra != 0) {
    [self setNeedsDisplay];
    [grid removeAllObjects];
    [self calculateGrid];
    counter = counter + extra;
    clear = YES;
    extra = 0;
}

for(int i=0; i<counter; i++) {
    int count = [grid count];
    NSLog(@"counter %i", count);
    if(count != 144) {
        srandom(time(NULL));
        int r = random() % [coordinates count];

        Coordinate *coordinate = [coordinates objectAtIndex:r];


        Square *square = [Square new];
        [square setSquareRect: CGRectMake ([coordinate x] * 25, [coordinate y] * 25, 20, 20)];
        [square setColor: [UIColor randomColor]];
        [writeSquares addObject:square];
        [grid addObject:square];
        [square release];

        [self setNeedsDisplayInRect:CGRectMake ([coordinate x] * 25, [coordinate y] * 25, 20, 20)];

        [coordinates removeObjectAtIndex:r];
        [coordinate release];
    }
    else {
        extra++;
    }
}

//[newlock release];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-08-25 06:58:47

我不知道为什么这会起作用;在我看来,您的所有 -setNeedsDisplayInRect: 调用都将合并为一个更新。我猜您希望每个方块的外观一个接一个地“突然”出现?

如果是这种情况,您需要给操作系统时间来循环其事件循环(这些调用只会在当前事件循环之后强制屏幕更新。)

您应该将循环移至单独的方法中,并使用以下任一方法调用它: -performSelector:withObject:afterDelay:,或 NSTimer。

I'm not sure why this ever worked; it seems to me that all of your -setNeedsDisplayInRect: calls will be coalesced into one update. I'm guessing you'd like the appearance of each square 'popping' in, one after another?

If this is the case, you need to give the OS time to cycle its event loop (these calls will only force a screen update after the current event cycle.)

You should move your loop into a separate method, and call it with either a -performSelector:withObject:afterDelay:, or an NSTimer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文