无论您的计时有多准确,您都不会让设备的显示频率超过大约 60Hz(每帧 16.7 毫秒)。在我看来,您至少有两个选择:
1)使用CADisplayLink
回调检查音频的播放进度,并在每个动画及时触发。
显示链接计时器的创建方式与常规类似NSTimers
:
-(void)viewDidLoad
{
// ...
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(scheduleAnimations:)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)scheduleAnimations:(CADisplayLink *)displayLink
{
// get playback time from player object
// iterate queue of timings
// if the timestamp of a timing is equal to or less than the playback time
// create its animation for immediate execution
// remove it from the queue
}
2)创建动画集合,将它们各自的beginTime
属性设置为适当的触发时间(或者,如果使用隐式或基于块的动画,则使用delay
参数)。
[CATransaction begin];
// iterate collection of timings
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"key"];
animation.startTime = /* time to trigger animation */
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:nil];
[CATransaction commit];