我有一组 UIImageView 的动画,它们代表当前值并不断向上滴答作响...理想情况下永远不会停止,视图中也是一个滚动视图或滚动视图,每当我滚动或缩放滚动视图时,动画都会停止,并且当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)
{
}];
}
As danypata https://stackoverflow.com/users/2315974/danypata/通过此线程在我的评论中指出当 UIScrollView 滚动时,我的自定义 UI 元素不会更新 https://stackoverflow.com/questions/4109898/my-custom-ui-elements-are-not-being-updated-while-uiscrollview-is-scrolled/4136988#4136988它与 NStimer 线程有关,而不是与动画线程有关,或者如果有人可以澄清的话,可能两者都有关。在任何情况下,当滚动时,似乎所有滚动事件都独占主循环,解决方案是将用于执行动画的计时器置于相同的循环模式中,即UITracking循环模式 http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Run_Loop_Mode_for_Tracking所以它也可以在滚动时使用主循环......
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:foreveryNseconds target:self selector:@selector(AscendDigit:) userInfo:dict repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Tada.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)