【问题标题】:Why does the animation change the color of the image?为什么动画会改变图像的颜色?
【发布时间】:2019-09-27 09:57:01
【问题描述】:

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

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

【问题讨论】:

    标签: swift uiimage core-animation uiviewanimation


    【解决方案1】:

    为你试试这个按钮:

    button.adjustsImageWhenHighlighted = false
    

    【讨论】:

      【解决方案2】:

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

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

      请更改您的动画时间

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多