gpt4 book ai didi

ios - 使用 CAShapeLayer 多次动画圆圈进度

转载 作者:可可西里 更新时间:2023-11-01 01:07:03 27 4
gpt4 key购买 nike

我需要在用户点击按钮时为圆圈的进度设置动画。我目前正在使用 CAShapeLayerCABasicAnimation。代码如下:

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var circleContainerView: UIView!

var progressPts = [0.1, 0.3, 0.7, 0.5]
let circleLayer = CAShapeLayer()

override func viewDidLoad() {
super.viewDidLoad()
setupCircle()
}

@IBAction func didTapAnimate(_ sender: Any) {
guard let pt = progressPts.first else { return }
let animateStroke = CABasicAnimation(keyPath: "strokeEnd")
animateStroke.toValue = pt
animateStroke.duration = 2.0
animateStroke.fillMode = .forwards
animateStroke.isRemovedOnCompletion = false
circleLayer.add(animateStroke, forKey: "MyAnimation")
progressPts.removeFirst()
}

private func setupCircle() {
circleLayer.frame = CGRect(origin: .zero, size: circleContainerView.bounds.size)
let center = CGPoint(x: circleLayer.bounds.width / 2.0,
y: circleLayer.bounds.height / 2.0)
circleLayer.path = UIBezierPath(arcCenter: center,
radius: (circleLayer.bounds.height / 2.0) - 5.0,
startAngle: 0,
endAngle: 2 * CGFloat.pi, clockwise: true).cgPath
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.lineWidth = 10.0
circleLayer.strokeColor = UIColor.red.cgColor
circleLayer.strokeEnd = 0.0
circleContainerView.layer.addSublayer(circleLayer)
}

}

为了在动画结束时笔画保持在正确的位置,我还需要将 fillMode .forwardsisRemovedOnCompletion 设置为 。这适用于第一个动画。然而,在下一个动画中,冲程首先重置:

enter image description here

我在这里做错了什么?如何在不重置的情况下为每个新的进度位置设置动画?此外,将 isRemovedOnCompletion 设置为 false 肯定不是一个好主意,因为该层最终会附加多个动画。

最佳答案

我刚刚使用 from value 属性完成了一个没有重置的圆圈,只需使用下面的代码,它就会流畅地制作动画

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var circleContainerView: UIView!
var progressPts = [0.1, 0.3, 0.7, 0.5]
var previousPts = 0.0
let circleLayer = CAShapeLayer()
var animateStroke: CABasicAnimation!
override func viewDidLoad() {
super.viewDidLoad()
setupCircle()
}

@IBAction func didTapAnimate(_ sender: Any) {
guard let pt = progressPts.first else { return }
animateStroke.fromValue = previousPts
animateStroke.toValue = pt
animateStroke.duration = 2.0
animateStroke.fillMode = .forwards
animateStroke.isRemovedOnCompletion = false
circleLayer.add(animateStroke, forKey: "MyAnimation")
previousPts = progressPts.removeFirst()
}

private func setupCircle() {
animateStroke = CABasicAnimation(keyPath: "strokeEnd")
circleLayer.frame = CGRect(origin: .zero, size: circleContainerView.bounds.size)
let center = CGPoint(x: circleLayer.bounds.width / 2.0,
y: circleLayer.bounds.height / 2.0)
circleLayer.path = UIBezierPath(arcCenter: center,
radius: (circleLayer.bounds.height / 2.0) - 5.0,
startAngle: 0,
endAngle: 2 * CGFloat.pi, clockwise: true).cgPath
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.lineWidth = 10.0
circleLayer.strokeColor = UIColor.red.cgColor
circleLayer.strokeEnd = 0.0
circleContainerView.layer.addSublayer(circleLayer)
}

}

输出

enter image description here

关于ios - 使用 CAShapeLayer 多次动画圆圈进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56945102/

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