首页 > 代码库 > ios animation的暂停pause、恢复resume

ios animation的暂停pause、恢复resume

项目中要用到动画的控制,比如说暂停、恢复继续等,先看代码:(很容易实现)
-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

如果在一个view中我们添加了多个动画,(不是组合动画),同样容易实现动画的暂停、恢复,方法就是为每个动画层layer调用上面的方法,比如我的方法如下,我用的是三个单独动画的组合效果,就是如下调用的


-(void)stopAnimation;
{
    NSLog(@"停止动画");
    //[_customView.layer removeAllAnimations];
    [self pauseLayer:_customView1.layer];
    [self pauseLayer:_customView2.layer];
    [self pauseLayer:_customView3.layer];
}

-(void)resumeAnimation
{
    [self resumeLayer:_customView1.layer];
    [self resumeLayer:_customView2.layer];
    [self resumeLayer:_customView3.layer];

}