我有一个带有渐变颜色效果的CALayer。我想更改"位置"和"颜色"值以更改渐变。我正在尝试为它制作动画。
但是使用以下代码,它只会立即更改为新值。
let gradientDuration = 0.25
let locationAnimation = CABasicAnimation(keyPath: "locations")
locationAnimation.fromValue = lowerGradientView.layer.locations
locationAnimation.toValue = newLocations
let colorsAnimation = CABasicAnimation(keyPath: "colors")
colorsAnimation.fromValue = lowerGradientView.layer.colors
colorsAnimation.toValue = newColors
let animationGroup = CAAnimationGroup()
animationGroup.duration = gradientDuration
animationGroup.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
animationGroup.animations = [locationAnimation, colorsAnimation]
lowerGradientView.layer.add(animationGroup, forKey: "locationAndColors")
lowerGradientView.layer.locations = newLocations as [NSNumber]?
lowerGradientView.layer.colors = newColors
我做错了什么导致图层不对更改进行动画处理?
问题是最后的这些行:
lowerGradientView.layer.locations = newLocations as [NSNumber]?
lowerGradientView.layer.colors = newColors
实际上,它们取消了动画。