gpt4 book ai didi

swift - 使用 CABasicAnimation 将半径从 0 调整为 100

转载 作者:行者123 更新时间:2023-11-28 05:50:18 26 4
gpt4 key购买 nike

我想为一个圆圈制作动画。圆半径应该从 0 增加到 100。我用 transform.scale 动画试过了。但是当然不可能缩放半径为 0 的圆。当我将圆的半径设置为 1 时,圆是可见的,尽管它不应该在动画的开头。最小的例子:

let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0
circleAnimation.toValue = 100
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)

在动画结束时,半径应保持在新值。

编辑:感谢@Rob mayoff

最终代码:

let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 400, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0.0000001
circleAnimation.toValue = 1
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)
circleAnimation.fillMode = .backwards

最佳答案

可能您想要做的是将 circlePath 的半径设置为您的最终半径:

let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
circlePath.clone()

然后从接近零的数字到 1.0 设置动画:

circleAnimation.fromValue = 0.0001
circleAnimation.toValue = 1.0

如果您想让动画在以后开始(通过设置 beginTime),那么您可能还想设置填充模式以便应用 fromValue到图层直到动画开始:

circleAnimation.fillMode = .backwards

关于swift - 使用 CABasicAnimation 将半径从 0 调整为 100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53214495/

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