0

I've read a million posts on stackoverflow about nstimers, runloops and the main thread but they don't seem to have the same problem or answer I'm looking for. My situation:

  1. I'm trying to animate some stuff while there is some 'loading going on'. The animation consists of a couple of scrollviews going back and forth using the contentOffset. I do this animation using an NSTimer.
  2. The 'loading going on' of point 1 is in fact not some simple loading, but some drawing on some other UIViews not currently visible to the user.

So to conclude, I'm trying to animate some scrollviews while drawing at the same time on UIViews. Is this even possible without them conflicting in such a way the the animation is not smooth? If I use a activityIndicatorView it's definitely possible, the spinning wheel never stutters, it's always spinning smoothly. I've tried to use runloops but I couldn't figure it out. Here's some of my code so far:

-(void)startLoading
{
    [self performSelectorInBackground:@selector(showSpinningCells)];
    [self drawOnViews];
}

-(void)drawOnViews
{
    //HERE A LOT OF VIEWS ARE BEING UPDATED AND CALLED SETNEEDSDISPLAY
}

-(void)spinCells
{
    //HERE I USE PERFORMSELECTORONMAINTHREAD TO SET THE
    //CONTENTOFFSET OF THE SCROLLVIEWS
}

-(void)showSpinningCells
{    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    spinLoop = [NSRunLoop currentRunLoop];
    spinTimer = nil;
    self.userInteractionEnabled = FALSE;
    spinTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 
                            target:self 
                          selector:@selector(spinCells) 
                          userInfo:nil 
                           repeats:YES] retain];    
    [spinLoop run]; 
    [pool release];
}

Please tell me if this is even possible before I try anything further and if so, how? Currently I never see any spinning. I do see spinning if I don't do anything afterwards, but as soon as I call the drawing on the UIViews the spinning completely stops.

Bob de Graaf
  • 2,630
  • 1
  • 25
  • 43

2 Answers2

0

You can try this,

-(void)showSpinningCells
{    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    spinTimer = nil;
    self.userInteractionEnabled = FALSE;
    spinTimer = [[NSTimer timerWithTimeInterval:0.2
                                        target:self
                                      selector:@selector(spinCells) 
                                      userInfo:nil
                                        repeats:YES] retain];
    [[NSRunLoop currentRunLoop] addTimer:spinTimer forMode:NSRunLoopCommonModes];
    [pool release];
}

add timer to currentRunLoop for NSRunLoopCommonModes

xda1001
  • 2,449
  • 16
  • 18
  • hi, I do think I've tried this but I'll try again, thanks for your reply. Also, you DO think it's possible what I want? – Bob de Graaf May 24 '12 at 11:36
0

UI animation can only happen after your drawOnViews method exits, so that the UI run loop can get the time needed to do animation in the main thread. One solution is to chop up your drawOnViews method into really short asynchronous segments, each of which quickly exits so that a UI animation can continue in between each segment.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • ah thanks, I thought so. There is only one thread, the main, that does all things. So if you want two drawings, you really need to time and chop them up. thanks! :) – Bob de Graaf May 25 '12 at 09:16