【发布时间】:2016-12-07 16:40:44
【问题描述】:
我正在为CAGradientLayer 制作动画,类似于 Apple 在 iPhone 主屏幕上的“滑动解锁”动画。然而,我的动画有点不同,它会在某些点减速和加速。
到目前为止,我的代码是动画渐变并且可以工作,但是我如何让它在不同的点减速/加速?
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)
}
}
更新 1:
为了让它看起来完全像我想要的那样,我需要使用CAMediaTimingFunction 吗?
【问题讨论】:
-
你需要一个关键帧动画。
-
@JF 怎么样 - 我还会使用
CAGradientLayer并将其与CAKeyframeAnimation结合使用吗? -
与您现在使用
CABasicAnimation的方式相同,但改为使用CAKeyframeAnimation。 -
@JF 好的,我明白你的意思了。我以前使用过
CAKeyframeAnimation,但那是沿着给定的path对UIView进行动画处理。不知道如何将它与CAGradientLayer一起使用。您能否显示一些代码或指向我的链接?谢谢
标签: ios objective-c swift core-animation cagradientlayer