通过以固定间隔更改的path属性SKShapeNode,可以创建逐帧动画序列。要创建动画,设置该path属性为形状的顺序,与一个圆,两端开始什么也没有。您可以使用UIBezierPath(用于的包装器)CGPath通过以下步骤为动画创建形状:
- 将路径的“笔”移动到圆心
- 弧添加到与路径addArcWithCenter从startAngle到endAngle
- 在圆弧上与终点角相对应的点到中心的路径上添加一条线
- 更改endAngle固定金额
重复步骤1-4
这是上述步骤的实现:override func didMove(to:SKView) {
let circle = SKShapeNode(circleOfRadius: 50)circle.fillColor = SKColor.bluecircle.strokeColor = SKColor.clearcircle.zRotation = CGFloat.pi / 2addChild(circle)countdown(circle: circle, steps: 20, duration: 5) { print("done")}}
// Creates an animated countdown timer
func countdown(circle:SKShapeNode, steps:Int, duration:TimeInterval, completion:@escaping ()->Void) {
guard let path = circle.path else {
return
}
let radius = path.boundingBox.width/2
let timeInterval = duration/TimeInterval(steps)
let incr = 1 / CGFloat(steps)
var percent = CGFloat(1.0)let animate = SKAction.run { percent -= incr circle.path = self.circle(radius: radius, percent:percent)}let wait = SKAction.wait(forDuration:timeInterval)let action = SKAction.sequence([wait, animate])run(SKAction.repeat(action,count:steps-1)) { self.run(SKAction.wait(forDuration:timeInterval)) { circle.path = nil completion() }}}
// Creates a CGPath in the shape of a pie with slices missing
func circle(radius:CGFloat, percent:CGFloat) -> CGPath {
let start:CGFloat = 0
let end = CGFloat.pi * 2 * percent
let center = CGPoint.zero
let bezierPath = UIBezierPath()
bezierPath.move(to:center)
bezierPath.addArc(withCenter:center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
bezierPath.addLine(to:center)
return bezierPath.cgPath
}



