如何使用UIView.animate在swift中制作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
})动画外观的屏幕截图:

发布于 2019-12-03 04:42:18
您需要使用CAKeyFrameAnimation,它有一个path属性,将用作动画的基础。可以使用bezier路径来定义U形。
下面是一个简单的示例,演示如何在屏幕上动态显示视图条目
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")
}
}我对如何让视图停留在层动画结束时的最终位置不满意,因为它不是100%无缝的。我知道使用委托不是我以前做过的事情,但我想不起来我以前是怎么做的,现在也没有时间去尝试它。希望其他人能提供更新?
编辑:更新以删除委托方法,并替换为.fillMode = .forwards,这是我当时想不到的!
https://stackoverflow.com/questions/59146069
复制相似问题