2
是否可以使用新的iOS 10 UIViewPropertyAnimator在循環路徑中爲視圖設置動畫效果?如何使用iOS 10在圓形路徑上創建視圖UIViewPropertyAnimator
我知道,它可以與CAKeyframeAnimation做到按How do I animate a UIView along a circular path?
我怎樣才能實現與新的API相同的結果?
是否可以使用新的iOS 10 UIViewPropertyAnimator在循環路徑中爲視圖設置動畫效果?如何使用iOS 10在圓形路徑上創建視圖UIViewPropertyAnimator
我知道,它可以與CAKeyframeAnimation做到按How do I animate a UIView along a circular path?
我怎樣才能實現與新的API相同的結果?
我結束了以下。基本上,計算圓上的點並使用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()