Animación de degradado: reduce la velocidad y acelera
Estoy animando unCAGradientLayer
, similar a como lo hizo Apple con su animación "Deslizar para desbloquear" en la pantalla de inicio del iPhone. Sin embargo, mi animación es un poco diferente, ya que se ralentiza y se acelera en ciertos puntos.
El código que tengo hasta ahora es que anima un degradado y funciona, pero ¿cómo podría hacer que se desacelere / acelere en diferentes puntos?
class AnimatedMaskLabel: UIView {
let gradientLayer: CAGradientLayer = {
let gradientLayer = CAGradientLayer()
// Configure the gradient here
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
let colors = [
UIColor.black.cgColor,
UIColor.white.cgColor,
UIColor.black.cgColor
]
gradientLayer.colors = colors
let locations: [NSNumber] = [0.0, 0.5, 1.0]
gradientLayer.locations = locations
return gradientLayer
}()
@IBInspectable var text: String! {
didSet {
setNeedsDisplay()
}
}
override func layoutSubviews() {
layer.borderColor = UIColor.green.cgColor
gradientLayer.frame = bounds
}
override func didMoveToWindow() {
super.didMoveToWindow()
layer.addSublayer(gradientLayer)
let gradientAnimation = CABasicAnimation(keyPath: "locations")
gradientAnimation.fromValue = [0.0, 0.0, 0.25]
gradientAnimation.toValue = [0.75, 1.0, 1.0]
gradientAnimation.duration = 3.0
gradientAnimation.repeatCount = Float.infinity
gradientLayer.add(gradientAnimation, forKey: nil)
}
}
Actualización 1:
Para que se vea exactamente como quiero, ¿necesitaría usarCAMediaTimingFunction
¿en absoluto?