我正在处理由CALayers组成的自定义视图,但我无法为其设置动画。
我的申请是结帐流程。每个屏幕都是结帐购物车中的一个项目。当用户通过购物车到达发票时,新控制器被推入堆栈。我使用我的进度视图来更新用户在购物车中的位置。
我可以更新进度视图,但是我无法将其设置为动画。将在每个新视图控制器上重新创建进度视图,并将其填充到指定的量。
//creates the shape layer, grayed with spaces in between
func createShapeLayer(_ count: Int) {
//calculate width of the view
let width = self.frame.width
//divide by the number of items(count) = single progress length
let singleProgressLength = width/CGFloat(count)
// take 10% off that as the gap length
let gapLength = singleProgressLength * 0.10
//new single progress length = progress length - gap
var newSingleProgressLength : CGFloat = 0.0
let staticWidth = singleProgressLength - gapLength
//loop through the count
for i in 1...count {
let layer = CAShapeLayer()
//start at 0 for the first layer
layer.path = UIBezierPath(roundedRect: CGRect(x: 0 + (newSingleProgressLength + gapLength), y: 0, width: staticWidth, height: self.frame.height), cornerRadius: 1).cgPath
newSingleProgressLength += (staticWidth + gapLength)
if i <= coloredUpToIndex {
layer.fillColor = UIColor(netHex:0xF7B445).cgColor
}else {
layer.fillColor = UIColor(netHex:0xD8D8D8).cgColor
}
self.layer.addSublayer(layer)
layerArray.append(layer)
}
}
// Adds increment to the progress view, filling a rectangle with animation
func animateViewUpdate() {
var delay : TimeInterval = 0.5
for layer in layerArray {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { () -> Void in
let animation = CABasicAnimation(keyPath: "opacity")
animation.duration = 1.0
animation.fromValue = NSNumber(value: 0.0 as Float)
animation.toValue = NSNumber(value: 1.0 as Float)
layer.add(animation, forKey: "animateOpacity")
self.perform(#selector(ECProgressView.animateViewUpdate), with: nil, afterDelay: delay)
}
delay += 0.5
}
}