我有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
})
})
请更改动画时间