Skip to content

Latest commit

 

History

History
60 lines (31 loc) · 1 KB

8.第八题.md

File metadata and controls

60 lines (31 loc) · 1 KB

8.如何暂停一个 UIView 中正在播放的动画?暂停后如何恢复?

实现方案是按照这篇博客写的 : https://blog.csdn.net/yuanyuan1314521/article/details/50033171

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{
    _number++;
    
    if (_number % 2 == 1) {

        [self pauseLayer:self.view.layer];
    }

    if (_number % 2 == 0) {

        [self resumeLayer:self.view.layer];
    }
}



//暂停动画

- (void)pauseLayer:(CALayer*)layer

{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];

    layer.speed = 0.0;

    layer.timeOffset = pausedTime;
}



//继续layer上面的动画
-(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;
}