【问题标题】:Shake view with UIVIewPropertyAnimator使用 UIVIewPropertyAnimator 摇动视图
【发布时间】:2021-09-01 19:30:52
【问题描述】:

我正在尝试使用完成处理程序编写抖动功能。当我调用函数时,我可以看到抖动本身,但它的幅度远小于期望值。请查看代码并帮助我修复错误。

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()
}

【问题讨论】:

    标签: swift uikit uiviewpropertyanimator


    【解决方案1】:

    使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      相关资源
      最近更新 更多