Layer动画的停止和恢复

自己写了2个方法,分别调用它们来实现layer层动画的暂停和恢复动画,

//动画的暂停实现方法
-(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];
 
}

你可能感兴趣的:(IOS)