【发布时间】:2019-03-22 19:21:26
【问题描述】:
我想在路径旁边(不带CAKeyframeAnimation)而不是UIViewPropertyAnimator 为视图设置动画。
为此,我有一条带有起点和终点的路径以及一个用于制作四曲线的控制点。
let path = UIBezierPath()
path.move(to: view.center)
path.addQuadCurve(to: target.center, controlPoint: CGPoint(x: view.center.x-10, y: target.center.y+160))
我的动画师使用 UIView.animateKeyframes 动画来为路径旁边的位置设置动画。
let animator = UIViewPropertyAnimator(duration: duration, curve: .easeIn)
animator.addAnimations {
UIView.animateKeyframes(withDuration: duration, delay: 0, options: [.calculationModeLinear], animations: {
let points = 100
for i in 1...points {
let pos = Double(i)/Double(points)
let x = self.quadBezier(pos: pos,
start: Double(view.center.x),
con: Double(view.center.x-10),
end: Double(target.center.x))
let y = self.quadBezier(pos: pos,
start: Double(view.center.y),
con: Double(target.center.y+160),
end: Double(target.center.y))
let duration = 1.0/Double(points)
let startTime = duration * Double(i)
UIView.addKeyframe(withRelativeStartTime: startTime, relativeDuration: duration) {
view.center = CGPoint(x: x, y: y)
}
}
})
}
为了计算分数,我使用与@rmaddy 相同的函数,认为我们这里有相同的source。
func quadBezier(pos: Double, start: Double, con: Double, end: Double) -> Double {
let t_ = (1.0 - pos)
let tt_ = t_ * t_
let tt = pos * pos
return Double(start * tt_) + Double(2.0 * con * t_ * pos) + Double(end * tt)
}
首先我认为计算是错误的,因为感觉计算的点通过控制点什么的:
但是在遍历相同的for循环并将蓝点添加到计算的x/y位置后表明位置是正确的。
所以我想知道为什么我的动画不遵循这条路径。
我找到了这个question 并发布了我的问题作为答案,但我想没有人认识到这一点。
【问题讨论】:
-
当然!我推了我的操场github.com/kuemme01/PathAnimationTest
标签: ios swift animation uibezierpath cgpoint