Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 nothing more than a kind of "float" value to specify seconds.

Or what else would be then the difference to the timeOffset property that is specified in CAMediaTiming protocol?

like image 205
Thanks Avatar asked Jul 22 '09 16:07

Thanks


2 Answers

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; 
like image 69
Ortwin Gentz Avatar answered Oct 13 '22 21:10

Ortwin Gentz


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") 
like image 41
Chris Avatar answered Oct 13 '22 22:10

Chris