我正在使用https://github.com/gordoneliel/LinearProgressBar 。我想将其制作成动画。
这是实现绘图方法的部分代码:
func drawProgressView() {
guard let context = UIGraphicsGetCurrentContext() else {return}
let beginPoint = CGPoint(x: barPadding + trackOffset, y: frame.size.height / 2)
// Progress Bar Track
drawOn(
context: context,
lineWidth: barThickness + trackPadding,
begin: beginPoint,
end: CGPoint(x: frame.size.width - barPadding - trackOffset, y: frame.size.height / 2),
lineCap: .round,
strokeColor: trackColor
)
// Progress bar
let colorForBar = barColorForValue?(Float(progressValue)) ?? barColor
drawOn(
context: context,
lineWidth: barThickness,
begin: beginPoint,
end: CGPoint(x: barPadding + trackOffset + calculatePercentage(), y: frame.size.height / 2),
lineCap: .round,
strokeColor: colorForBar
)
}
这里可以添加动画吗?
我找到了如何将其添加到进度 View :self.progressView.setProgress(1.0,animated:true)。但我想将其添加到 https://github.com/gordoneliel/LinearProgressBar .
最佳答案
由于您正在处理图层,因此您可以尝试 CATransaction。
您可以将图层重绘包含在这样的动画 block 中
UIView.transition(with: myView, duration: 1, options: .transitionCrossDissolve, animations: {
myView.layer.displayIfNeeded()
})
或者尝试将您的drawOn 封装在CATransaction 事务中,如下所示
CATransaction.begin()
CATransaction.setAnimationDuration(0.25)
drawOn(
context: context,
lineWidth: barThickness,
begin: beginPoint,
end: CGPoint(x: barPadding + trackOffset + calculatePercentage(), y: frame.size.height / 2),
lineCap: .round,
strokeColor: colorForBar
)
CATransaction.commit()
希望这有帮助。
关于ios - 如何动画进度 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51760172/