放慢 CAGradientLayer 动画
Slowing down CAGradientLayer animation
我想为三色渐变设置动画慢慢地。
我有一个习惯 UIView
像这样:
class MyView: UIView, CAAnimationDelegate {
lazy var gradientLayer: CAGradientLayer = {
let l = CAGradientLayer()
l.frame = self.frame
l.colors = self.colors
l.startPoint = CGPoint(x: 0, y: 0)
l.endPoint = CGPoint(x: 1, y: 0)
l.mask = self.textLayer
return l
}()
lazy var textLayer: CATextLayer = {
let l = CATextLayer()
l.frame = self.frame
l.string = "test".uppercased()
l.fontSize = 23
return l
}()
var colors: [CGColor] = [
UIColor.red.cgColor,
UIColor.purple.cgColor,
UIColor.blue.cgColor
]
init() {
...
self.layer.addSublayer(gradientLayer)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func animate() {
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")
a.toValue = newColors
a.duration = 0.1
a.isRemovedOnCompletion = true
a.fillMode = kCAFillModeForwards
a.delegate = self
gradientLayer.add(a, forKey:"animateGradient")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
animate()
}
}
这有效,但速度非常快。我怎样才能放慢动画速度,而不会变得块状?
我是否必须向 colors
数组添加几百种颜色,以便它有足够的颜色循环?
谢谢
除非我在您的代码中遗漏了某些内容,否则您尚未在动画中设置 fromValue。当然,在将颜色切换为 newColors 之前,您需要将其设置为以前的颜色值:
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")
}
这将是您没有真正获得动画的原因 - 您的代码已有效设置以创建延迟的捕捉过渡。
我想为三色渐变设置动画慢慢地。
我有一个习惯 UIView
像这样:
class MyView: UIView, CAAnimationDelegate {
lazy var gradientLayer: CAGradientLayer = {
let l = CAGradientLayer()
l.frame = self.frame
l.colors = self.colors
l.startPoint = CGPoint(x: 0, y: 0)
l.endPoint = CGPoint(x: 1, y: 0)
l.mask = self.textLayer
return l
}()
lazy var textLayer: CATextLayer = {
let l = CATextLayer()
l.frame = self.frame
l.string = "test".uppercased()
l.fontSize = 23
return l
}()
var colors: [CGColor] = [
UIColor.red.cgColor,
UIColor.purple.cgColor,
UIColor.blue.cgColor
]
init() {
...
self.layer.addSublayer(gradientLayer)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func animate() {
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")
a.toValue = newColors
a.duration = 0.1
a.isRemovedOnCompletion = true
a.fillMode = kCAFillModeForwards
a.delegate = self
gradientLayer.add(a, forKey:"animateGradient")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
animate()
}
}
这有效,但速度非常快。我怎样才能放慢动画速度,而不会变得块状?
我是否必须向 colors
数组添加几百种颜色,以便它有足够的颜色循环?
谢谢
除非我在您的代码中遗漏了某些内容,否则您尚未在动画中设置 fromValue。当然,在将颜色切换为 newColors 之前,您需要将其设置为以前的颜色值:
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")
}
这将是您没有真正获得动画的原因 - 您的代码已有效设置以创建延迟的捕捉过渡。