渐变animation – 慢下来,加快速度
我为CAGradientLayer
了一个animation,类似于苹果公司在iPhone主屏幕上的“幻灯片解锁”animation。 不过,我的animation有点不同,因为它在某些方面减速并加快速度。
我到目前为止的代码是animation渐变和工作,但我怎么会得到它减速/加快在不同的点?
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
吗?
要使用关键帧animation,请尝试:
let gradientAnimation = CAKeyframeAnimation(keyPath: "locations") gradientAnimation.values = [[0.0, 0.0, 0.25], [0.375, 0.5, 0.625], [0.75, 1.0, 1.0]] gradientAnimation.duration = 3.0 gradientAnimation.repeatCount = Float.infinity gradientLayer.add(gradientAnimation, forKey: nil)
这将在三次平均之间。 要更改关键帧发生的时间,请设置keyTimes
:
gradientAnimation.keyTimes = [0.0, 0.4, 1.0]
这将设置应该为每个元素传递animation的百分比,以反映animation的当前状态。 这也应该具有与values
相同的长度。
我实际上不知道Swift,所以这应该工作,但我不能保证。
- iOS – 在Swift中用NSJSONSerializationparsingJSON字典
- 把价值传递给closures?
- 如何使用Swift检查iPhone是否处于飞行模式
- Alamofire Image:在af_setImageWithURL()之后从AutoPurgingImageCache获取图像
- 如何在导航栏或其他屏幕内容上方创build视图?
- Swift 3:如何在video上添加水印? AVVideoCompositionCoreAnimationTool iOS 10问题
- ios – Swift – dequeueReusableCellWithReuseIdentifier,collectionViewController的单元格(在UIView中)滚动后不会加载
- UiView.animateWithDuration不能animationSwift
- 在Swift中从CVPixelBufferRef获取像素值