views:

423

answers:

4

I'm calling setNeedsDisplayInRect from an NSTimer function every two seconds. This works perfectly, and draws a square in a random position, with a random color. However, in some circumstances I would like to draw a square x number of times in the NSTimer function (using a for loop) - but, after numerous error testing it seems that drawRect is only called once even though I am running setNeedsDisplayInRect x number of times? I would love some help as I've been trying to figure out this problem all day long. Carl.

Edit below is my code...

View

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
    CGContextSetFillColorWithColor(context, currentColor.CGColor);
    CGContextAddRect(context, redrawRect);
    CGContextDrawPath(context, kCGPathFillStroke);
}

-(void)drawInitializer 
{
    int x = (int)arc4random() % [self.xCoordinates count]; 
    int y = (int)arc4random() % [self.yCoordinates count]; 
    self.currentColor = [UIColor randomColor];
    self.redrawRect = CGRectMake ([[self.xCoordinates objectAtIndex:x] intValue],       [[self.yCoordinates objectAtIndex:y] intValue], 25, 25);
    [self setNeedsDisplayInRect:redrawRect];
}

Controller

- (void) handleTimer: (NSTimer *) timer
{
    for(int i=0; i<5; i++) 
    {
         [self.squareView drawInitializer];
    }
}
A: 

Do you determine the position of the square inside the drawRect: method? If so, you should change it. You could for example create an NSRect instance variable that gets drawn by the drawRect: method, BUT the actual position of the NSRect is set in another method (e.g. your NSTimer function).

Oh, and you should probably post your code so people can more easily help you out!

Miles
A: 

Removed code, added to main body

Carl
-1 This is info pertaining to your original question. Edit your question and put it in there. It does not belong as an answer.
Dave DeLong
lol, you were quick. I was doing it as you made the comment.
Carl
A: 

You could refactor the code so that you have a simple class that:

  • stores color
  • stores position
  • has a method to generate random positions, colors, ...

You could then create as many instances as you want and push them into a NSMutableArray.
This way you can iterate over that list, and draw each object in your draw routine.
Whenever you add/delete/modify one of your objects, call setNeedsDisplay:

weichsel
thanks, works like a treat :)
Carl
A: 

you could create a class that has the ability to draw itself, then create as many instances of this class that you need.

darren