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

问题描述 投票:0回答:1

我有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)
    }
  }

当我使用此功能时,在动画中间,我的图像变为灰色(但图像本来是白色的)。我不明白为什么会这样。

enter image description here

swift uiimage core-animation uiviewanimation
1个回答
0
投票
嗨,我希望这对动画有帮助。>

//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 }) })

请更改动画时间
© www.soinside.com 2019 - 2024. All rights reserved.