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

ぐ巨炮叔叔 提交于 2019-12-02 16:17:15

What I missed in the docs: beginTime is in "core animation absolute time" so you've to get the current time and specify your offset from that:

// Start in 5 seconds
theAnimation.beginTime = CACurrentMediaTime()+5;
Chris

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")

No, that is not what beginTime does. It specifies a relative start time from its parent animation (by default multiple animations in a group all fire at once).

From the documentation:

Specifies the begin time of the receiver in relation to its parent object, if applicable.

timeOffset causes it to start animating at the frame it would be at at the offSet time, and when it reaches the end it loops around. In other words, imagine A,B,C,D,E are frames of animation this is what happends in various cases if you set beginTime or timeOffset to a value equal to when you hit frame C in the normal case.

Normal      |A->B->C->D->E
beginTime:  |      A->B->C->D->E
timeOffset: |C->D->E->A->B

I think the documentation of CAMediaTiming Protocol is very bad. Time Warp in Animation is a thorough explanation(re-documentation) of all properties of CAMediaTiming Protocol.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!