我正在尝试使用完成处理程序编写抖动功能。当我调用函数时,我可以看到抖动本身,但它的幅度远小于期望值。请查看代码并帮助我修复错误。
func shake(_ completion: @escaping () -> Void) {
let animator = UIViewPropertyAnimator(duration: 0.6, curve: .linear)
let distances: [CGFloat] = [-20.0, 20.0, -20.0, 20.0, -10.0, 10.0, -5.0, 5.0, 0.0 ]
for (i, value) in distances.enumerated() {
let delayFactor = Double(i) / Double(distances.count)
animator.addAnimations({ [center, weak self] in
self?.center = CGPoint(x: center.x + value, y: center.y)
},
delayFactor: CGFloat(delayFactor))
}
animator.addCompletion { _ in completion() }
animator.startAnimation()
}