UIScrollView滚动事件阻止UIView动画

Gen*_*ain 9 animation multithreading uiscrollview ios

我有一个UIImageView的动画集代表当前值并且不断向上滴答...理想情况下永远不会停止,在视图中也是一个scrollView或者当我滚动或放大scrollView时,动画停止,并且当scrollView完全停止移动时再次启动.我认为这是由于一个线程问题,因为重绘元素都发生在主线程上,起初我尝试了UIView动画,然后甚至核心动画都没有效果......有没有办法让我的蛋糕也吃掉它?

任何和所有的帮助将不胜感激

代码如下

- (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)
     {

     }];

}
Run Code Online (Sandbox Code Playgroud)

Gen*_*ain 29

正如 danypata在我的评论中通过这个帖子指出我的自定义UI元素在UIScrollView滚动时没有被更新它与NStimer线程而不是动画线程有关,或者如果有人可以澄清那么两者都有.在任何情况下,当滚动时似乎所有滚动事件都得到主循环的独占使用,解决方案是将你用来做动画的定时器放到UITrackingLoopMode的相同循环模式中,这样它也可以使用主循环滚动和......

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

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Run Code Online (Sandbox Code Playgroud)

田田.