【问题标题】:Animate a view alongside a path without CAKeyframeAnimation在不使用 CAKeyframeAnimation 的情况下为路径旁边的视图设置动画
【发布时间】: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 并发布了我的问题作为答案,但我想没有人认识到这一点。

【问题讨论】:

标签: ios swift animation uibezierpath cgpoint


【解决方案1】:

View.center 在动画过程中发生变化,固定就没有问题了:

try the following:

        let cent = view.center
        UIView.animateKeyframes(withDuration: duration, delay: 0, options: [.beginFromCurrentState], animations: {
            let points = 100

            for i in 1...points {

                let pos = Double(i)/Double(points)
                let x = self.quadBezier(pos: pos,
                                        start: Double(cent.x), //here
                                        con: Double(cent.x-10), //here
                                        end: Double(square.center.x))
                let y = self.quadBezier(pos: pos,
                                        start: Double(cent.y), //here
                                        con: Double(square.center.y+160),
                                        end: Double(square.center.y))

                let duration = 1.0 / Double(points)
                let startTime = duration * Double(i)
                print("Animate: \(Int(x)) : \(Int(y))")
                UIView.addKeyframe(withRelativeStartTime: startTime, relativeDuration: duration) {
                   view.center = CGPoint(x: x, y: y)

                }
            }
        })
    }

【讨论】:

  • 有道理,我不会这么说的!非常感谢......再次!
猜你喜欢
  • 2016-01-29
  • 1970-01-01
  • 2022-11-07
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多