我有一个CAShapeLayer()
,上面有一个渐变色,正在对其进行动画处理,但是在某种程度上,它看起来像下面的图片:
它看起来如何?
我的代码:
override func viewDidLayoutSubviews() {
displayLine()
}
override func viewDidAppear(_ animated: Bool) {
animateStroke()
}
func displayLine() {
let trackLayer = CAShapeLayer()
let rect = CGRect(x: topView.frame.width * 0.15, y: topView.frame.size.height / 1.5, width: topView.frame.width * 0.7, height: 2)
let path = UIBezierPath(roundedRect: rect, cornerRadius: 1)
trackLayer.path = path.cgPath
trackLayer.strokeColor = UIColor.groupTableViewBackground.cgColor
trackLayer.lineWidth = 3
trackLayer.fillColor = UIColor.clear.cgColor
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.lineWidth = 4
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeEnd = 0
topView.layer.addSublayer(trackLayer)
topView.layer.addSublayer(shapeLayer)
let color = UIColor(red: 11/255, green: 95/255, blue: 244/255, alpha: 1).cgColor
let sndColor = UIColor(red: 255/255, green: 87/255, blue: 87/255, alpha: 1).cgColor
gradient.colors = [color, sndColor]
gradient.locations = [0.0, 1.0]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.frame = topView.bounds
gradient.mask = shapeLayer
topView.layer.addSublayer(gradient)
}
func animateStroke() {
if !animated {
animated = true
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
var value: Double?
let distance = currLeasingCar!.currentKm - currLeasing!.startKm
value = Double(distance) / Double(finalKm)
basicAnimation.toValue = value
basicAnimation.duration = 1.5
basicAnimation.fillMode = .forwards
basicAnimation.isRemovedOnCompletion = false
basicAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
shapeLayer.add(basicAnimation, forKey: "lineStrokeAnimation")
}
}
答案 0 :(得分:4)
问题在于您的路径是一个圆角矩形。在您与我们分享的图片中,描边大概是2-3%。将其更改为90%的笔划,您会看到它试图绘制一个宽且非常短的圆角矩形,例如:
相反,只需将路径设置为一条线,它将按预期工作:
let path = UIBezierPath()
let bounds = topView.bounds
path.move(to: CGPoint(x: bounds.minX + bounds.width * 0.15, y: bounds.minY + bounds.height / 1.5))
path.addLine(to: CGPoint(x: bounds.minX + bounds.width * 0.85, y: bounds.minY + bounds.height / 1.5))
您可能还想对形状图层的帽进行圆角处理:
trackLayer.lineCap = .round // or whatever you want
shapeLayer.lineCap = .round
当然,此更改丢失了原始路径的2点高度,因此,如果要使这些形状层更厚,只需增加它们各自的lineWidth
值即可。
一些不相关的观察结果
viewDidLayoutSubviews()
和viewDidAppear(_:)
应该调用其super
实现。
viewDidLayoutSubviews()
可以被多次调用,因此您不想每次都实例化一个新的trackLayer
。或者,如果需要,请确保删除前一个。
在添加子视图/子图层时,请谨慎使用bounds
而不是frame
。在这种情况下,这可能并不重要,但是在某些情况下,您会遇到各种奇怪的问题,因为frame
位于视图的超级视图的坐标系中,而bounds
是视图中的视图的坐标系问题。
就个人而言,如果您要将这段代码保存在视图控制器中,我建议:
viewDidLoad
中添加形状图层和渐变; viewDidLayoutSubviews
更新渐变的路径和边界; 更好的是,所有这些动画代码实际上根本不属于应用程序的视图控制器,而是属于UIView
子类(或子视图控制器)。
因此,也许:
@IBDesignable
public class GradientProgressView: UIView {
private var shapeLayer: CAShapeLayer = {
let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineCap = .round
return shapeLayer
}()
private var trackLayer: CAShapeLayer = {
let trackLayer = CAShapeLayer()
trackLayer.strokeColor = UIColor.groupTableViewBackground.cgColor
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = .round
return trackLayer
}()
private var gradient: CAGradientLayer = {
let gradient = CAGradientLayer()
let color = UIColor(red: 11/255, green: 95/255, blue: 244/255, alpha: 1).cgColor
let sndColor = UIColor(red: 255/255, green: 87/255, blue: 87/255, alpha: 1).cgColor
gradient.colors = [color, sndColor]
gradient.locations = [0.0, 1.0]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
return gradient
}()
override init(frame: CGRect = .zero) {
super.init(frame: frame)
addSubLayers()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addSubLayers()
}
override public func layoutSubviews() {
super.layoutSubviews()
updatePaths()
}
override public func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setProgress(0.75, animated: false)
}
public func setProgress(_ progress: CGFloat, animated: Bool = true) {
if animated {
animateStroke(to: progress)
} else {
shapeLayer.strokeEnd = progress
}
}
}
private extension GradientProgressView {
func addSubLayers() {
layer.addSublayer(trackLayer)
layer.addSublayer(shapeLayer)
layer.addSublayer(gradient)
}
func updatePaths() {
let lineWidth = bounds.height / 2
trackLayer.lineWidth = lineWidth * 0.75
shapeLayer.lineWidth = lineWidth
let path = UIBezierPath()
path.move(to: CGPoint(x: bounds.minX + lineWidth / 2, y: bounds.midY))
path.addLine(to: CGPoint(x: bounds.maxX - lineWidth / 2, y: bounds.midY))
trackLayer.path = path.cgPath
shapeLayer.path = path.cgPath
gradient.frame = bounds
gradient.mask = shapeLayer
}
func animateStroke(to progress: CGFloat) {
let key = "lineStrokeAnimation"
layer.removeAnimation(forKey: key)
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = progress
basicAnimation.duration = 1.5
basicAnimation.fillMode = .forwards
basicAnimation.isRemovedOnCompletion = false
basicAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
shapeLayer.add(basicAnimation, forKey: key)
}
}
然后视图控制器仅仅是:
class ViewController: UIViewController {
@IBOutlet weak var gradientProgressView: GradientProgressView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateProgress()
}
...
}
// MARK: - Progress related methods
private extension ViewController {
func updateProgress() {
let distance = currLeasingCar!.currentKm - currLeasing!.startKm
let value = CGFloat(distance) / CGFloat(finalKm)
gradientProgressView.setProgress(value)
}
}