gpt4 book ai didi

ios - 在 Swift 中动画绘制圆圈的奇怪问题

转载 作者:行者123 更新时间:2023-11-28 06:10:30 25 4
gpt4 key购买 nike

我正在尝试用动画绘制一个圆圈:

func drawProgressCircle(with endAngle: CGFloat) {
let progressBezierPath = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.width / 2), radius: self.frame.width / 2.5, startAngle: -CGFloat(Double.pi / 2), endAngle: endAngle, clockwise: true)
let progressShapeLayer = CAShapeLayer()
progressShapeLayer.path = progressBezierPath.cgPath

progressShapeLayer.strokeColor = UIColor.brown.cgColor
progressShapeLayer.fillColor = UIColor.clear.cgColor
progressShapeLayer.lineWidth = 15.0

// Do not draw initially. Wait for animation
progressShapeLayer.strokeEnd = 0.0

// Make corners rounded
progressShapeLayer.lineCap = kCALineCapRound

self.layer.addSublayer(progressShapeLayer)
self.animate(circleWith: progressShapeLayer, from: -CGFloat(Double.pi / 2), to: endAngle, with: 2.0)
}

为了让它动起来,我调用了这个函数:

func animate(circleWith shapeLayer: CAShapeLayer, from: CGFloat, to: CGFloat, with duration: TimeInterval) {
// We want to animate the strokeEnd property of the circleLayer
let animation = CABasicAnimation(keyPath: "strokeEnd")

// Set the animation duration appropriately
animation.duration = duration

// Animate from start point to end point
animation.fromValue = from
animation.toValue = to

// Do a linear animation (i.e. the speed of the animation stays the same)
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

// Set the circleLayer's strokeEnd property to 1.0 now so that it's the
// right value when the animation ends.
shapeLayer.strokeEnd = 1.0

// Do the actual animation
shapeLayer.add(animation, forKey: "animate")
}

但我不知道为什么它没有动画。 Canvas 是空的,2 秒后它就出现在 Canvas 上。没有任何动画。

问题是什么?为什么它没有动画绘图?

最佳答案

将动画 formValue 和 toValue 替换为:

// Animate from start point to end point
animation.fromValue = 0
animation.toValue = 1

基本上 keyPath strokeCABasicAnimation 并不知道您正在绘制一个圆,它只是从 0 到 1 进行动画处理。如果你给 toValue 一个 0.5 的值,它只会动画到路径的一半。

它不同于 CABasicAnimation for keyPath position,它采用 CGPoint 作为 fromValuetoValue

关于ios - 在 Swift 中动画绘制圆圈的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46863493/

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