我想制作一个圆的动画。圆的半径应该从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

10-07 19:51
查看更多