我想在图8的运动中设置UIView的动画。我正在使用BezierPath在Swift中执行此操作,但我想为动画添加延迟。

let center: CGPoint = CGPoint(x: 60, y: 45)
let cent: CGPoint = CGPoint(x: 90, y: 45)
let radius: CGFloat = 15.0
let start: CGFloat = CGFloat(M_PI)
let end: CGFloat = 0.0
let path1: UIBezierPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
let path2: UIBezierPath = UIBezierPath(arcCenter: cent, radius: radius, startAngle: -start, endAngle: end, clockwise: false)
let path3: UIBezierPath = UIBezierPath(arcCenter: cent, radius: radius, startAngle: end, endAngle: -start, clockwise: false)
let path4: UIBezierPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: end, endAngle: start, clockwise: true)
let paths: UIBezierPath = UIBezierPath()
paths.appendPath(path1)
paths.appendPath(path2)
paths.appendPath(path3)
paths.appendPath(path4)

let anim: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")
anim.path = paths.CGPath
anim.repeatCount = 2.0
anim.duration = 3.0

view.layer.addAnimation(anim, forKey: "animate position along path")

图8工作得很好,但我不知道如何添加延迟。我试过使用类似于animateWithDuration(delay:options:animation:completion:)的方法,但没有帮助。如果我离基地太远,有一个更容易的方法动画一个UIView在一个'图8'/'无限循环'的运动,这将是好的。我只需要动作加上延迟。

最佳答案

动画对象有一个开始时间。不管你想要多少,把它设定在未来。

anim.beginTime = CACurrentMediaTime() + 1.0

此示例将延迟启动1秒。
使用dispatch_after块启动整个动画方法在短期内可能会获得相同的结果,但可能会抑制相对于未来的开始时间调整或操作动画上附加计时函数的能力。调整动画本身上的时间可以保持动画对象的计时状态一致。

07-24 09:50
查看更多