我正在尝试使用完成处理程序编写摇动函数。当我调用函数时,我可以看到抖动本身,但它的大小比预期值要小得多。请看一下代码,帮我修复这个bug。
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()
}发布于 2021-09-01 19:55:49
使用addAnimations方法添加的动画与任何以前配置的动画(source: Apple Documentation)一起运行。因此,在你的例子中,你所有的动画都试图相互抵消。
如果您正在寻找类似弹簧的动画,请尝试使用dampingRatio初始化动画生成器
myView.transform = CGAffineTransform(translationX: -20, y: 0)
let animator = UIViewPropertyAnimator(duration: 1.0, dampingRatio: 0.1) {
self.myView.transform = .identity
}
animator.startAnimation()如果这不是您想要的,那么您可能想要使用keyframe animation。
https://stackoverflow.com/questions/69019608
复制相似问题