【问题标题】:How to animate view on circular path with iOS 10 UIViewPropertyAnimator如何使用 iOS 10 UIViewPropertyAnimator 在圆形路径上为视图设置动画
【发布时间】:2017-02-10 08:28:39
【问题描述】:

是否可以使用新的 iOS 10 UIViewPropertyAnimator 在圆形路径中为视图设置动画?

我知道可以按照 How do I animate a UIView along a circular path? 使用 CAKeyframeAnimation 来完成

如何使用新 API 实现相同的结果?

【问题讨论】:

    标签: ios animation ios10


    【解决方案1】:

    我最终得到了以下结果。本质上,计算圆上的点并使用 animateKeyFrames 在它们之间移动。

    let radius = CGFloat(100.0)
    let center = CGPoint(x: 150.0, y: 150.0)
    let animationDuration: TimeInterval = 3.0
    let animator = UIViewPropertyAnimator(duration: animationDuration, curve: .linear)
    animator.addAnimations {
        UIView.animateKeyframes(withDuration: animationDuration, delay: 0, options: [.calculationModeLinear], animations: {
            let points = 1000
            let slice = 2 * CGFloat.pi / CGFloat(points)
    
            for i in 0..<points {
                let angle = slice * CGFloat(i)
                let x = center.x + radius * CGFloat(sin(angle))
                let y = center.y + radius * CGFloat(cos(angle))
    
                let duration = 1.0/Double(points)
                let startTime = duration * Double(i)
                UIView.addKeyframe(withRelativeStartTime: startTime, relativeDuration: duration) {
                    ninja.center = CGPoint(x: x, y: y)
                }
            }
        })
    }
    animator.startAnimation()
    

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      相关资源
      最近更新 更多