gpt4 book ai didi

ios - 随着时间的推移,时间目标可变的进度条

转载 作者:行者123 更新时间:2023-11-30 11:21:47 26 4
gpt4 key购买 nike

我对 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")
}

最佳答案

您只需要重新配置持续时间,因此假设您希望它填充 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")

})

关于ios - 随着时间的推移,时间目标可变的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51186803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com