当uiscrollview事件发生时,NSTimer不会被触发

我有一个UIImageView放置在UIScrollView,基本上这个UIImageView拥有非常大的地图,并创build了一个预定义的path与“箭头”指向导航方向的animation。

但是,每当uiscrollevents发生,我认为MainLoop冻结和NSTimer不被解雇,animation停止。

在UIScrollView,CAKeyFrameAnimation或NSTimer中是否有解决这个问题的现有属性?

//viewDidLoad self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(drawLines:) userInfo:nil repeats:YES]; - (void)drawLines:(NSTimer *)timer { CALayer *arrow = [CALayer layer]; arrow.bounds = CGRectMake(0, 0, 5, 5); arrow.position = CGPointMake(line.x1, line.y1); arrow.contents = (id)([UIImage imageNamed:@"arrow.png"].CGImage); [self.contentView.layer addSublayer:arrow]; CAKeyframeAnimation* animation = [CAKeyframeAnimation animation]; animation.path = path; animation.duration = 1.0; animation.rotationMode = kCAAnimationRotateAuto; // object auto rotates to follow the path animation.repeatCount = 1; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; animation.fillMode = kCAFillModeForwards; [arrow addAnimation:animation forKey:@"position"]; } 

iOS应用程序在NSRunLoop上运行。 每个NSRunLoop对于不同的任务有不同的执行模式。 例如,默认的nstimer计划在NSRunLoop上的NSDefaultRunMode下运行。 这意味着什么是某些UIEvents,scrollviewing是一个,将中断计时器,并将其放置在队列中,一旦事件停止更新运行。 在你的情况下,为了让定时器不被中断,你需要安排一个不同的模式,即NSRunLoopCommonModes,如下所示:

  self.myTimer = [NSTimer scheduledTimerWithTimeInterval:280 target:self selector:@selector(doStuff) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes]; 

这种模式将使您的计时器不会被滚动中断。 你可以在这里find关于这个信息的更多信息: https : //developer.apple.com/documentation/foundation/nsrunloop在最下面你会看到你可以select的模式的定义。 此外,传说有它,你可以写你自己的自定义模式,但很less有人曾经生活告诉这个故事不怕。