title: Re-Animate Gradient Background with different color in swift in ios
body: <p>I want to re-animate gradient background with different color when animating.
  I can successfully animate gradient color with this code.</p>
  
  <pre><code>let dayTopColor = CommonUtils.colorWithHexString("955EAC")
  let dayBottomColor = CommonUtils.colorWithHexString("9F3050")
  let dayToTopColor = CommonUtils.colorWithHexString("D15B52")
  let dayToBottomColor = CommonUtils.colorWithHexString("CC4645")
  let nightTopColor = CommonUtils.colorWithHexString("2D5E7C")
  let nightBottomColor = CommonUtils.colorWithHexString("19337D")
  let nightToTopColor = CommonUtils.colorWithHexString("21334E")
  let nightToBottomColor = CommonUtils.colorWithHexString("101A55")
  var isInSaudiArabia = false
  var gradient : CAGradientLayer?
  var toColors : AnyObject?
  var fromColors : AnyObject?
  
  func animateBackground(){
          var layerToRemove: CAGradientLayer?
          for layer in self.view.layer.sublayers!{
              if layer.isKindOfClass(CAGradientLayer) {
                  layerToRemove = layer as? CAGradientLayer
              }
          }
          layerToRemove?.removeFromSuperlayer()
  
              self.gradient!.colors = [nightTopColor.CGColor, nightBottomColor.CGColor]
              self.toColors = [nightToTopColor.CGColor, nightToBottomColor.CGColor]
  
          self.view.layer.insertSublayer(self.gradient!, atIndex: 0)
          animateLayer()
      }
  
      func animateLayer(){
  
          self.fromColors = self.gradient!.colors!
          self.gradient!.colors = self.toColors as? [AnyObject]
          let animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
          animation.delegate = self
          animation.fromValue = fromColors
          animation.toValue = toColors
          animation.duration = 3.50
          animation.removedOnCompletion = true
          animation.fillMode = kCAFillModeForwards
          animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
          animation.delegate = self
  
          self.gradient!.addAnimation(animation, forKey:"animateGradient")
      }
  
      override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
  
          self.toColors = self.fromColors;
          self.fromColors = self.gradient!.colors!
  
          animateLayer()
      }
  </code></pre>
  
  <p>CommonUtils.colorWithHexString() is a function which converts hex color to UIColor.
  Btw when i try to change background color to day color while animating, background gradient color got flickered.</p>
  
  <p>Is there anybody who knows solution.</p>