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.
            Asked
            
        
        
            Active
            
        
            Viewed 2.9k times
        
    63
            
            
        
        Paulo Mattos
        
- 18,845
 - 10
 - 77
 - 85
 
        openfrog
        
- 40,201
 - 65
 - 225
 - 373
 
- 
                    I would just use GCD. ` dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ <#code to be executed on the main queue after delay#> });` – BooRanger Feb 27 '13 at 16:22
 - 
                    1@BooRanger what is wrong with the `beginTime` property? – David Rönnqvist Feb 27 '13 at 16:32
 - 
                    [This answer](http://stackoverflow.com/a/11625742/608157) should answer your question – David Rönnqvist Feb 27 '13 at 16:36
 
2 Answers
160
            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
- 
                    77
 - 
                    2I am not able to use this for AVVideoCompositionCoreAnimationTool. The only thing which works is AVCoreAnimationBeginTimeAtZero. I want some delay for my animation... – nr5 Nov 13 '14 at 05:24
 - 
                    
 
22
            
            
        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
        Dave G
        
- 12,042
 - 7
 - 57
 - 83