gpt4 book ai didi

ios - 动画 UIView 到 "squiggle"到目的地的方式而不是直线前进

转载 作者:行者123 更新时间:2023-11-28 05:43:53 25 4
gpt4 key购买 nike

我有这样的动画:

bubble.frame.origin = CGPoint(x: 75, y: -120)

UIView.animate(
withDuration: 2.5,
animations: {
self.bubble.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height * -1.3)
}
)

但是,动画是直线进行的。我希望动画在到达目的地的途中来回移动一点。像一个泡沫。有什么想法吗?

最佳答案

如果您想沿路径设置动画,您可以使用 CAKeyframeAnimation。唯一的问题是您想要什么样的路径。阻尼正弦曲线可能就足够了:

func animate() {
let box = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
box.backgroundColor = .blue
box.center = bubblePoint(1)
view.addSubview(box)

let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = bubblePath().cgPath
animation.duration = 5
box.layer.add(animation, forKey: nil)
}

在哪里

private func bubblePath() -> UIBezierPath {
let path = UIBezierPath()
path.move(to: bubblePoint(0))
for value in 1...100 {
path.addLine(to: bubblePoint(CGFloat(value) / 100))
}

return path
}

/// Point on curve at moment in time.
///
/// - Parameter time: A value between 0 and 1.
/// - Returns: The corresponding `CGPoint`.

private func bubblePoint(_ time: CGFloat) -> CGPoint {
let startY = view.bounds.maxY - 100
let endY = view.bounds.minY + 100
let rangeX = min(30, view.bounds.width * 0.4)
let midX = view.bounds.midX

let y = startY + (endY - startY) * time
let x = sin(time * 4 * .pi) * rangeX * (0.1 + time * 0.9) + midX
let point = CGPoint(x: x, y: y)
return point
}

产量:

enter image description here

关于ios - 动画 UIView 到 "squiggle"到目的地的方式而不是直线前进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55621425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com