进度条具有随时间变化的时间目标

时间:2018-07-05 08:37:43

标签: ios swift

我是Swift的新手,也是编码方面的菜鸟,所以这可能真的很简单。我正在创建一个进度圈,当24小时过去时,我希望将其计数为100%,然后重置进度并进行计数,直到从原始起点经过树天为止,然后是一周……到目前为止,我已经圈出并可以手动控制进度,但是我不确定从哪里开始获得时间来控制进度。希望有人能够提供帮助。

lass ViewController: UIViewController {

let shapeLayer = CAShapeLayer()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let center = view.center

    // Create my track
    let trackLayer = CAShapeLayer()
    let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
    trackLayer.path = circularPath.cgPath

    trackLayer.strokeColor = UIColor.lightGray.cgColor
    trackLayer.lineWidth = 20
    trackLayer.fillColor = UIColor.clear.cgColor
    trackLayer.lineCap = kCALineCapRound
    view.layer.addSublayer(trackLayer)

    // Create progress circle
    shapeLayer.path = circularPath.cgPath

    shapeLayer.strokeColor = UIColor.red.cgColor
    shapeLayer.lineWidth = 20
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.lineCap = kCALineCapRound

    shapeLayer.strokeEnd = 0

    view.layer.addSublayer(shapeLayer)

    print ("Atempting to animate stroke")

    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")

    basicAnimation.toValue = 1

    basicAnimation.duration = 1.2

    basicAnimation.fillMode = kCAFillModeForwards
    basicAnimation.isRemovedOnCompletion = false

    shapeLayer.add(basicAnimation, forKey: "urSoBasic")
}

1 个答案:

答案 0 :(得分:0)

您只需要重新配置duration,那么假设您希望它在60秒内完成填充,然后在10秒内再次重新填充即可,您可以这样做(可以创建一个函数,但这只是为了说明,您也可以使用计时器以使其轻松失效)

let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")

basicAnimation.toValue = 1

basicAnimation.duration = 60

basicAnimation.fillMode = kCAFillModeForwards

basicAnimation.isRemovedOnCompletion = false

shapeLayer.add(basicAnimation, forKey: "urSoBasic")

DispatchQueue.main.asyncAfter(deadline: .now() + 60 , execute: {

    self.shapeLayer.removeAllAnimations()

    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")

    basicAnimation.toValue = 1

    basicAnimation.duration = 10

    basicAnimation.fillMode = kCAFillModeForwards

    basicAnimation.isRemovedOnCompletion = false

    self.shapeLayer.add(basicAnimation, forKey: "urSoBasic")

})