Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 563587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:40:34+00:00 2026-05-13T12:40:34+00:00

I have written a function that draws x number of squares in a random

  • 0

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];

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:40:34+00:00Added an answer on May 13, 2026 at 12:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function that gets a given number of random records from
I have written a javascript function that uses setInterval to manipulate a string every
I have written a function that will transform a number in base 10 to
I have written a function that positions a tooltip just above a textbox. The
I have written code to perform a function that could take a while to
I have written a PHP function to take a video embed code that has
I currently have a function written called saveWorkout that saves an NSMutableArray to another
I have the view function in django that written like a dispatcher calling other
I have written a function that checks if to files are duplicates or not.
I have written a function that creates a barplot. I would like to save

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.