为什么动画会改变图像的颜色?

Why does the animation change the color of the image?

我有 func,它可以改变图像并产生一些效果:

  private func changeStarIcon(from: String, to: String) {
    UIView.animateKeyframes(withDuration: 0.5, delay: 0.0, options: UIView.KeyframeAnimationOptions.calculationModeCubic, animations: {
      UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.2, animations: {
        (self.navigationItem.rightBarButtonItems?[0].customView as? UIButton)?.imageView?.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
      })
      UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.2, animations: {
        (self.navigationItem.rightBarButtonItems?[0].customView as? UIButton)?.imageView?.transform = CGAffineTransform.identity
        let fadeAnim = CABasicAnimation(keyPath: "contents")
        fadeAnim.fromValue = UIImage(named: from)
        fadeAnim.toValue = UIImage(named: to)
        fadeAnim.duration = 0.3
        (self.navigationItem.rightBarButtonItems?[0].customView as? UIButton)?.imageView?.layer.add(fadeAnim, forKey: "contents")
        (self.navigationItem.rightBarButtonItems?[0].customView as? UIButton)?.setImage( UIImage(named: to)?.withRenderingMode(.alwaysOriginal), for: .normal)
      })
    }) { success in
      (self.navigationItem.rightBarButtonItems?[0].customView as? UIButton)?.setImage( UIImage(named: to)?.withRenderingMode(.alwaysOriginal), for: .normal)
    }
  }

当我使用这个函数时,在动画中间我的图像变成灰色(但图像原本是白色的)。我不明白为什么会这样。

嗨,我希望这对你的动画有所帮助

 //First animation
  UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.2, 
 animations: {

     //write Your code 


    }, completion: { (finished: Bool) in

                //Second Animation

     UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.2, 
     animations: {

   //write YourCode

 }, completion: { (finished: Bool) in

                //Third Animation

            })


    })

请更改动画时间

试试这个按钮:

button.adjustsImageWhenHighlighted = false