Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView scroll event blocks UIView animation

I have an animating set of UIImageView's that represent a current value and continually keeps ticking upwards...ideally never stopping, also in the view is a scrollView or to and whenever I'm scrolling or zooming in the scrollView, the animation stops, and starts up again when the scrollView stops moving completely. I believe this is due to a threading issue since redrawing elements all happens on the main thread, At first i tried UIView animation and then even core animation to no effect...Is there a way to have my cake and eat it too?

Any and all help would be appreciated

code follows

- (void)TestJackpotAtRate:(double)rateOfchange
{
    double roc = rateOfchange;

    for (int i = 0; i < [_jackPotDigits count]; ++i)
    {
        roc = rateOfchange/(pow(10, i));

        UIImageView *jackpotDigit = [_jackPotDigits objectAtIndex:i];

        float foreveryNseconds = 1/roc;

        NSDictionary *dict = @{@"interval"      :   [NSNumber numberWithFloat:foreveryNseconds],
                           @"jackPotDigit"  :   jackpotDigit
                           };

        [NSTimer scheduledTimerWithTimeInterval:foreveryNseconds target:self selector:@selector(AscendDigit:) userInfo:dict repeats:YES];
    }
}

-(void)AscendDigit:(NSTimer*)timer
{
    NSDictionary *dict = [timer userInfo];

    NSTimeInterval interval = [(NSNumber*)[dict objectForKey:@"interval"] floatValue];
    UIImageView *jackpotDigit = [dict objectForKey:@"jackPotDigit"];

    float duration = (interval < 1) ? interval : 1;

    if (jackpotDigit.frame.origin.y < -230 )
    {
        NSLog(@"hit");
        [timer invalidate];
        CGRect frame = jackpotDigit.frame;
        frame.origin.y = 0;
        [jackpotDigit setFrame:frame];

        [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(AscendDigit:) userInfo:dict repeats:YES];
    }

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^
     {
         CGRect frame = [jackpotDigit frame];

         double yDisplacement = 25;

         frame.origin.y -= yDisplacement;

         [jackpotDigit setFrame:frame];

     }
                      completion:^(BOOL finished)
     {

     }];

}
like image 728
Genhain Avatar asked May 15 '13 05:05

Genhain


1 Answers

As danypata pointed out in my comments via this thread My custom UI elements are not being updated while UIScrollView is scrolled It has something to do with the NStimer thread rather than the animation thread, or possibly both if someone can clarify. In any case when scrolling it seems all scroll events get exclusive use of the main loop, the solution is to put the timer you are using to do your animation into the same loop mode which is UITrackingLoopMode so it too get's use of the main loop when scrolling and...

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:foreveryNseconds target:self selector:@selector(AscendDigit:) userInfo:dict repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

Tada.

like image 139
Genhain Avatar answered Nov 15 '22 19:11

Genhain