如何使用 UIView.animate 快速制作 U 形动画?我正在尝试将我的圆形图像动画化到 UIView
的中间,但我只是得到了只能直线移动的动画。我希望它下降然后上升并停在 UIView 的中间。
我尝试使用此代码:
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
infoImage.center.y += 15
infoImage.center.x = animationView.frame.size.width / 2
})
动画应该是什么样子的截图:
最佳答案
您需要使用 CAKeyFrameAnimation,它有一个路径属性,将用作动画的基础。您可以使用贝塞尔曲线路径来定义 U 形。
这是一个简单的例子,说明如何在屏幕上设置 View 的动画
class MyVC {
var movingView = UIView()
let endPoint = CGPoint(x: mainView.frame.width * 2/3, y: 200)
func animateViewEntry() {
movingView = UIView(frame: CGRect(-100,-100,100,100)) //intially off-screen
movingView.backgroundColor = .red
view.addSubview(movingView)
let animation = CAKeyframeAnimation(keyPath: "position")
let path = UIBezierPath()
path.move(to: CGPoint(x:-movingView.frame.width, y:50))
path.addQuadCurve(to: endPoint,
controlPoint: CGPoint(x:mainView.frame.width * 1/3, y: 350))
animation.path = path.cgPath
animation.duration = 2
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
movingView.layer.add(animiation, forKey: "Bounce")
}
}
我不满意如何让 View 在图层动画结束时停留在其最终位置,因为它不是 100% 无缝的。我知道使用委托(delegate)并不是我以前的做法,但我不记得我以前是怎么做的,而且我现在没有时间玩它。希望其他人提供更新?
已编辑:更新为删除委托(delegate)方法并替换为 .fillMode = .forwards
,这是我当时想不到的!
关于ios - 如何使用 UIView.animate 创建 U 形动画? - swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59146069/