【发布时间】:2017-05-12 07:05:49
【问题描述】:
我有进度方法here:
func progress(incremented : CGFloat){
if incremented <= self.bounds.width{
self.progressLayer.removeFromSuperlayer()
let originBezierPathProg = UIBezierPath(roundedRect: CGRect(x:0, y:0, width:0, height:self.bounds.height) , cornerRadius: self.viewCornerRadius)
originBezierPathProg.close()
let newBezierPathProg = UIBezierPath(roundedRect: CGRect(x:0, y:0, width:incremented, height:self.bounds.height) , cornerRadius: self.viewCornerRadius)
bezierPathProg.close()
self.progressLayer.path = originBezierPathProg.cgPath
self.borderLayer.addSublayer(self.progressLayer)
let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = originBezierPathProg.cgPath
animation.toValue = newBezierPathProg.cgPath
animation.duration = 1
self.progressLayer.add(animation, forKey: animation.keyPath)
self.progressLayer.path = newBezierPathProg.cgPath
}
}
我正在尝试以动画方式使进度条取得进展。但是当我调用progress(100) 时,它只是在渲染没有动画的栏。
我该如何解决?
更新:根据 Rob 的建议创建了 MCVE:https://github.com/utkarsh2012/ProgressBarTest。我希望进度条的动画从 width=0 到 width=x(比如 60)
看起来类似于这个问题CABasicAnimation with CALayer path doesn't animate
【问题讨论】:
-
虽然这个could be tightened up a bit,它对我有用,就这样。所以问题是你如何调用它(太早,太频繁,从后台线程),或者你的
bounds或incremented值使得if语句失败。我们需要问题的可生产示例。 -
让我想出一个解决这个问题的简单项目。现在已经为此苦苦挣扎了几个小时,我认为真正的问题是它是从 UIView 的 var ididSet 调用的(可能还为时过早?)。之后如果需要,我也会进行布局。查看 setupView:github.com/TwoPence/TwoPence/blob/milestone/TwoPence/…
-
很难说。那里有一些不相关的东西,但同时,我认为还不足以重现问题。如果您希望我们提供帮助,我们真的需要MCVE,即向我们展示如何创建空白项目,插入显示您问题的最少代码量。或者,如果您想自己诊断问题,请添加检查 (a) 确保它发生在主线程上的调试代码; (b)
bounds是您在调用progress(incremented:)时的想法; (c)incremented的价值是什么。 -
b 和 c 发生正确。我将创建一个空白项目并复制它。感谢您的帮助!
-
@Rob 创建为简单的 xcode 项目(VC 中的 UIView,与我当前的项目相同)及其可重现的 github.com/utkarsh2012/ProgressBarTest