How to delay a CABasicAnimation?

IosUikitCore AnimationCabasicanimation

Ios Problem Overview


I have a CABasicAnimation and want to start it after a delay. In UIKit I can specify delays. The CAMediaTiming protocol has a timeOffset property but I can't see an effect. My next try is to use GCD to delay it but it feels like overkill.

Ios Solutions


Solution 1 - Ios

Shouldn't you be using the [CAMediaTiming beginTime] property (reference)?

See Customizing the Timing of an Animation in the Core Animation Programming Guide.

CABasicAnimation *animation;
animation.beginTime = CACurrentMediaTime() + 0.3; //0.3 seconds delay

Solution 2 - Ios

In Swift 3.0:

func animateYourView () {
   let myDelay = 5.0
   let scalePulseAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
   scalePulseAnimation.beginTime = CACurrentMediaTime() + myDelay
   scalePulseAnimation.duration = 0.5
   scalePulseAnimation.repeatCount = 2.0
   scalePulseAnimation.autoreverses = true
   scalePulseAnimation.fromValue = 1.0
   scalePulseAnimation.toValue = 0.5
   myView.layer.add(scalePulseAnimation, forKey: "scale")
}

Where the key line for the delay is:

  scalePulseAnimation.beginTime = CACurrentMediaTime() + myDelay

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionopenfrogView Question on Stackoverflow
Solution 1 - IostrojanfoeView Answer on Stackoverflow
Solution 2 - IosDave GView Answer on Stackoverflow