我有一个带有渐变颜色效果的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
他们实际上取消了动画。
关于ios - 为什么我的CALayers不设置动画?取而代之的是,它们只是立即随新的值(value)而跃升。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56659350/