func animate() {
let oldColors = colors
var newColors = colors
let lastColor: CGColor = newColors.last!
newColors.removeLast()
newColors.insert(lastColor, at: 0)
colors = newColors
gradientLayer.colors = newColors
let a = CABasicAnimation(keyPath:"colors")
// New line here ->
a.fromValue = oldColors
a.toValue = newColors
a.duration = 0.1
a.isRemovedOnCompletion = true
a.fillMode = kCAFillModeForwards
a.delegate = self
gradientLayer.add(a, forKey:"animateGradient")
}
这将是您没有真正获得动画的原因-您的代码被有效地设置为创建延迟的捕捉转换。