How to specify an beginTime for an animation by using CFTimeInterval?

后端 未结 4 1337
情话喂你
情话喂你 2021-01-30 17:57

For my understanding, beginTime can be used to say \"hey, start at exactly 12:00 \'o clock\". But how would I tell this with an CFTimeInterval type? I thought that this one is n

4条回答
  •  独厮守ぢ
    2021-01-30 18:21

    You first need to convert to the layer's timespace like so:

    let currentLayerTime = myLayer.convertTime(CACurrentMediaTime(), from: nil)
    

    Then you can set the beginTime relative to the layer's now time. For instance, to make an animation begin in 2s:

    myAnimation.beginTime = currentLayerTime + 2
    

    You'll also likely want to set the fillMode to .backwards, so that you can set the final property value before you add the animation:

    myAnimation.fillMode = .backwards
    myLayer.someProperty = someFinalValue
    myLayer.addAnimation(myAnimation, forKey: "myAnimationName")
    

提交回复
热议问题