首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UIVIewPropertyAnimator摇动视图

使用UIVIewPropertyAnimator摇动视图
EN

Stack Overflow用户
提问于 2021-09-01 19:30:52
回答 1查看 48关注 0票数 0

我正在尝试使用完成处理程序编写摇动函数。当我调用函数时,我可以看到抖动本身,但它的大小比预期值要小得多。请看一下代码,帮我修复这个bug。

代码语言:javascript
复制
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()
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-01 19:55:49

使用addAnimations方法添加的动画与任何以前配置的动画(source: Apple Documentation)一起运行。因此,在你的例子中,你所有的动画都试图相互抵消。

如果您正在寻找类似弹簧的动画,请尝试使用dampingRatio初始化动画生成器

代码语言:javascript
复制
myView.transform = CGAffineTransform(translationX: -20, y: 0)
let animator = UIViewPropertyAnimator(duration: 1.0, dampingRatio: 0.1) {
    self.myView.transform = .identity
}
animator.startAnimation()

如果这不是您想要的,那么您可能想要使用keyframe animation

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69019608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档