title: CAAnimation with timeRange - iOS
body: <p>I need a <code>CALayer</code> to appear on screen at a specified time with some  <code>CAAnimation</code> ( eg. <em>fadeIn</em>) . I want it to stay on the screen for a couple of seconds and then disappear with a <code>fadeOut</code> animation. </p>
  
  <p><strong>Example</strong> : If I have a timeRange as :
  <code>CMTimeRangeMake(start: 3 , end : 5)</code> I need a <code>CAAnimation</code> at the start of 3 seconds and one at the end of  5 seconds .The CALayer must only be present during the duration of the <code>timeRange</code>.</p>
  
  <p>I have found a work around to display the <code>CALayer</code> so that it appears  at a specified time , however I don't know how to make it stay for a given duration. </p>
  
  <pre><code>        // Call this method in   viewDidLoad for quick demo
  
        func layerAnimation(){
  
      let box =             CALayer()
      box.frame =           CGRect(x:100, y: 100, width: 100, height: 100)
      box.backgroundColor = UIColor.orange.cgColor
       self.view.layer.addSublayer(box)
  
      // Animation
  
      CATransaction.begin()
  
      let hide = CABasicAnimation(keyPath: "opacity")
      hide.duration =  3                               // The Start time for the box to appear in seconds
      hide.fromValue = 0
      hide.toValue =   0
      hide.isRemovedOnCompletion = false
      hide.fillMode = kCAFillModeBoth
  
       CATransaction.setCompletionBlock({() -&gt; Void in
  
          let fadeInFadeOut = CABasicAnimation(keyPath: "opacity")
          fadeInFadeOut.duration =  0
          fadeInFadeOut.fromValue = 0
          fadeInFadeOut.toValue =   1
          fadeInFadeOut.isRemovedOnCompletion = false
          fadeInFadeOut.fillMode = kCAFillModeBoth
          fadeInFadeOut.autoreverses = true
  
  
          box.add(fadeInFadeOut, forKey: "fadeInFadeOut")
      })
      box.add(hide, forKey: "hide")
      CATransaction.commit()
  
  }
  </code></pre>
  
  <p>I finally want to be able to add titles to a video to make Lyric videos like <a href="https://www.youtube.com/watch?v=PT2_F-1esPk" rel="nofollow noreferrer">this one</a>.    </p>