动画 CAShapeLayer / CAGradientLayer
Animating a CAShapeLayer / CAGradientLayer
这是对我昨天提出的问题的补充。结果完美,但我想要修改。
下面的代码,基于上面的线程,生成了一个 CABasicAnimation 来生成深蓝色 "triangle." 我希望它不是深蓝色纯色,而是一个有 3 个色标的 CAGradientLayer: Red /绿色/蓝色。
我尝试添加一个 CAGradientLayer 来配置渐变,然后使用 addSubLayer 将渐变添加到动画出来的 CAShapeLayer。我在下面发布的结果没有动画。颜色和形状保持不变。渐变看起来不错,只需要它动画出来:
import UIKit
import QuartzCore
import XCPlayground
let view = UIView(frame: CGRect(x: 0, y: 0, width: 521, height: 40))
view.backgroundColor = UIColor.whiteColor()
XCPlaygroundPage.currentPage.liveView = view
let maskPath = UIBezierPath()
maskPath.moveToPoint(CGPoint(x: 0, y: 30))
maskPath.addLineToPoint(CGPoint(x: 0, y: 25))
maskPath.addLineToPoint(CGPoint(x: 300, y: 10))
maskPath.addLineToPoint(CGPoint(x: 300, y: 30))
maskPath.closePath()
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.CGPath
maskLayer.fillColor = UIColor.whiteColor().CGColor
let rectToAnimateFrom = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 0, height: 40))
let rectToAnimateTo = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 97, height: 40))
let backgroundGray = CAShapeLayer()
backgroundGray.path = maskPath.CGPath
backgroundGray.fillColor = UIColor.grayColor().CGColor
let foregroundGradient = CAShapeLayer()
foregroundGradient.mask = maskLayer
foregroundGradient.backgroundColor = UIColor.clearColor().CGColor
let gradientLayer = CAGradientLayer()
gradientLayer.startPoint = CGPointMake(0, 0)
gradientLayer.endPoint = CGPointMake(1, 0)
gradientLayer.frame = maskPath.bounds
let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor, UIColor.blueColor().CGColor]
gradientLayer.colors = colors
foregroundGradient.addSublayer(gradientLayer)
view.layer.addSublayer(backgroundGray)
view.layer.addSublayer(foregroundGradient)
let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = rectToAnimateFrom.CGPath
animation.toValue = rectToAnimateTo.CGPath
animation.duration = 2
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
foregroundGradient.addAnimation(animation, forKey: nil)
我需要 foregroundGradient 对象(带有 gradientLayer 子层!)从左到右设置动画。
如果您从我上面的代码中删除渐变,并将 foregroundGradient 保留为纯色,您将看到动画效果。
我认为您必须在您的视图中使用您的 mask
,如下所述。
//Remove below line
view.layer.addSublayer(backgroundGray)
//Add your mask to view
view.layer.mask = backgroundGray
整个函数如下所示:
func getGradientView() {
let view = UIView(frame: CGRect(x: 0, y: 50, width: 521, height: 40))
view.backgroundColor = UIColor.grayColor()
self.view.addSubview(view)
let maskPath = UIBezierPath()
maskPath.moveToPoint(CGPoint(x: 0, y: 30))
maskPath.addLineToPoint(CGPoint(x: 0, y: 25))
maskPath.addLineToPoint(CGPoint(x: 300, y: 10))
maskPath.addLineToPoint(CGPoint(x: 300, y: 30))
maskPath.closePath()
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.CGPath
maskLayer.fillColor = UIColor.whiteColor().CGColor
let rectToAnimateFrom = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 0, height: 40))
let rectToAnimateTo = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 300, height: 40))
let backgroundGray = CAShapeLayer()
backgroundGray.path = maskPath.CGPath
backgroundGray.fillColor = UIColor.grayColor().CGColor
let foregroundGradient = CAShapeLayer()
foregroundGradient.mask = maskLayer
foregroundGradient.backgroundColor = UIColor.clearColor().CGColor
let gradientLayer = CAGradientLayer()
gradientLayer.startPoint = CGPointMake(0, 0)
gradientLayer.endPoint = CGPointMake(1, 0)
gradientLayer.frame = maskPath.bounds
let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor, UIColor.blueColor().CGColor]
gradientLayer.colors = colors
foregroundGradient.addSublayer(gradientLayer)
//Remove below line
//view.layer.addSublayer(backgroundGray)
view.layer.addSublayer(foregroundGradient)
//Add you mask to view
view.layer.mask = backgroundGray
let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = rectToAnimateFrom.CGPath
animation.toValue = rectToAnimateTo.CGPath
animation.duration = 1
animation.repeatCount = 1000
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
maskLayer.addAnimation(animation, forKey: "fill animation")
}
让我知道它是否按照您的例外情况工作。
这是对我昨天提出的问题的补充。结果完美,但我想要修改。
下面的代码,基于上面的线程,生成了一个 CABasicAnimation 来生成深蓝色 "triangle." 我希望它不是深蓝色纯色,而是一个有 3 个色标的 CAGradientLayer: Red /绿色/蓝色。
我尝试添加一个 CAGradientLayer 来配置渐变,然后使用 addSubLayer 将渐变添加到动画出来的 CAShapeLayer。我在下面发布的结果没有动画。颜色和形状保持不变。渐变看起来不错,只需要它动画出来:
import UIKit
import QuartzCore
import XCPlayground
let view = UIView(frame: CGRect(x: 0, y: 0, width: 521, height: 40))
view.backgroundColor = UIColor.whiteColor()
XCPlaygroundPage.currentPage.liveView = view
let maskPath = UIBezierPath()
maskPath.moveToPoint(CGPoint(x: 0, y: 30))
maskPath.addLineToPoint(CGPoint(x: 0, y: 25))
maskPath.addLineToPoint(CGPoint(x: 300, y: 10))
maskPath.addLineToPoint(CGPoint(x: 300, y: 30))
maskPath.closePath()
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.CGPath
maskLayer.fillColor = UIColor.whiteColor().CGColor
let rectToAnimateFrom = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 0, height: 40))
let rectToAnimateTo = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 97, height: 40))
let backgroundGray = CAShapeLayer()
backgroundGray.path = maskPath.CGPath
backgroundGray.fillColor = UIColor.grayColor().CGColor
let foregroundGradient = CAShapeLayer()
foregroundGradient.mask = maskLayer
foregroundGradient.backgroundColor = UIColor.clearColor().CGColor
let gradientLayer = CAGradientLayer()
gradientLayer.startPoint = CGPointMake(0, 0)
gradientLayer.endPoint = CGPointMake(1, 0)
gradientLayer.frame = maskPath.bounds
let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor, UIColor.blueColor().CGColor]
gradientLayer.colors = colors
foregroundGradient.addSublayer(gradientLayer)
view.layer.addSublayer(backgroundGray)
view.layer.addSublayer(foregroundGradient)
let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = rectToAnimateFrom.CGPath
animation.toValue = rectToAnimateTo.CGPath
animation.duration = 2
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
foregroundGradient.addAnimation(animation, forKey: nil)
我需要 foregroundGradient 对象(带有 gradientLayer 子层!)从左到右设置动画。
如果您从我上面的代码中删除渐变,并将 foregroundGradient 保留为纯色,您将看到动画效果。
我认为您必须在您的视图中使用您的 mask
,如下所述。
//Remove below line
view.layer.addSublayer(backgroundGray)
//Add your mask to view
view.layer.mask = backgroundGray
整个函数如下所示:
func getGradientView() {
let view = UIView(frame: CGRect(x: 0, y: 50, width: 521, height: 40))
view.backgroundColor = UIColor.grayColor()
self.view.addSubview(view)
let maskPath = UIBezierPath()
maskPath.moveToPoint(CGPoint(x: 0, y: 30))
maskPath.addLineToPoint(CGPoint(x: 0, y: 25))
maskPath.addLineToPoint(CGPoint(x: 300, y: 10))
maskPath.addLineToPoint(CGPoint(x: 300, y: 30))
maskPath.closePath()
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.CGPath
maskLayer.fillColor = UIColor.whiteColor().CGColor
let rectToAnimateFrom = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 0, height: 40))
let rectToAnimateTo = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 300, height: 40))
let backgroundGray = CAShapeLayer()
backgroundGray.path = maskPath.CGPath
backgroundGray.fillColor = UIColor.grayColor().CGColor
let foregroundGradient = CAShapeLayer()
foregroundGradient.mask = maskLayer
foregroundGradient.backgroundColor = UIColor.clearColor().CGColor
let gradientLayer = CAGradientLayer()
gradientLayer.startPoint = CGPointMake(0, 0)
gradientLayer.endPoint = CGPointMake(1, 0)
gradientLayer.frame = maskPath.bounds
let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor, UIColor.blueColor().CGColor]
gradientLayer.colors = colors
foregroundGradient.addSublayer(gradientLayer)
//Remove below line
//view.layer.addSublayer(backgroundGray)
view.layer.addSublayer(foregroundGradient)
//Add you mask to view
view.layer.mask = backgroundGray
let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = rectToAnimateFrom.CGPath
animation.toValue = rectToAnimateTo.CGPath
animation.duration = 1
animation.repeatCount = 1000
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
maskLayer.addAnimation(animation, forKey: "fill animation")
}
让我知道它是否按照您的例外情况工作。