CAKeyframeAnimation Manual Progress

前端 未结 2 584
南方客
南方客 2021-02-04 20:31

I have a UIView whose backing layer has a CAKeyframeAnimation with a simple straight line path set as its `path`.
Can I have the animation \"frozen\", so to speak, and man

2条回答
  •  孤城傲影
    2021-02-04 20:37

    Yes you can do this with the CAMediaTiming interface. You can set the speed of the layer to 0 and manualy set the timeOffset. Example of a simple pause/resume method:

    - (void)pauseAnimation {
        CFTimeInterval pausedTime = [yourLayer convertTime:CACurrentMediaTime() fromLayer:nil];
        yourLayer.speed = 0.0;
        yourLayer.timeOffset = pausedTime;
    }
    
    - (void)resumeAnimation {
    
        CFTimeInterval pausedTime = [yourLaye timeOffset];
        if (pausedTime != 0) {
            yourLayer.speed = 1.0;
            yourLayer.timeOffset = 0.0;
            yourLayer.beginTime = 0.0;
    
            CFTimeInterval timeSincePause = [yourLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
            yourLayer.beginTime = timeSincePause;
        }
    }
    

提交回复
热议问题