1

I have a complex Quartz routine in drawRect for a custom UIView subclass. It can take a few seconds to draw. What I'd like is to show a UIActivityIndicator while it is drawing. But then the indicator must stop spinning (and get hidden) after the drawing is complete.

I tried to start the animation of the indicator and then use performSelector for a custom method that simply calls setNeedsDisplay - my thinking is that performSelector will wait until the next run loop, right? In which case, my indicator has time to start on the main thread. This seems to be working, but as soon as I add the code to the end of drawRect to stop the animation, the indicator doesn't show up at all, as if this animation is ending before it's had a chance to begin.

Any suggestions?

I call the drawing like this:

[self.spinner startAnimating];
[self performSelectorOnMainThread:@selector(redraw) withObject:nil waitUntilDone:YES];//tried both YES and NO here

-(void)redraw{
[self.customView setNeedsDisplay];

}

The drawRect: simply has this at the end:

 - (void)drawRect:(CGRect)rect{
  //bunch of drawing
  [self.nav stopSpinner]; // reference to a controller class
   }

In self.nav object is this:

 -(void)stopSpinner{
self.spinner.hidden=YES;
[self.spinner stopAnimating];
}

And the spinner object is initially created like this:

    self.spinner=[[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]autorelease];
    self.spinner.hidden=YES;
    [self.viewController.view addSubview:self.spinner];
4
  • 1
    Show us the drawRect: code that tries to stop the spinning.
    – rob mayoff
    Aug 9, 2012 at 6:42
  • it's quite long and I'd rather not post it here; however the very last line before the function's } is the message to the UIActivityIndicator for it to stop animating.
    – johnbakers
    Aug 9, 2012 at 7:18
  • Edit your question and paste in that line.
    – rob mayoff
    Aug 9, 2012 at 7:31
  • i found the issue and will be updating my question in a sec. the problem with the spinner not stopping is that I forgot to pass reference to the right object for self.nav but now the indicator is not showing up at all, likely because the drawing is still happening on the same run loop event as the indicator starts animating
    – johnbakers
    Aug 9, 2012 at 7:44

1 Answer 1

0

Perform your drawing on a background thread. Not only will this allow you to throw up an activity indicator and have it animate as desired, but the drawing itself with be much much faster. In my tests, I've seen a speed increase of 10X in complex Quartz drawing on a background thread.

And it's not hard. In fact, it might be so fast you won't need the activity indicator.

Here's the code, and you can simply use a regular UIImageView and then assign its image property to the rendered output from the other thread:

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

  dispatch_async(queue, ^{

    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    //perform some drawing into this context

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    dispatch_sync(dispatch_get_main_queue(), ^{
        self.imageView.image=viewImage;
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.