"Launch" 并放下一个 UIView

"Launch" and drop a UIView

上图说明了我想要的:

我尝试了什么:

对于启动,我可以使用 UIView.animate 函数。对于下降,我可以使用 UIDynamicAnimator。但是也有一些问题:

-在UIView.animate我不能'curve'动画,只有一条直线。我可以在这里使用这个答案画一条曲线:

-结合这两个功能不起作用。 UIView.animate 完成后,UIView 直接向下下降。

UIDynamicAnimator 代码:

var animator: UIDynamicAnimator!

    //view did appear
    let view2 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    view2.center = self.view.center
    view2.backgroundColor = .blue
    self.view.addSubview(view2)
    animator = UIDynamicAnimator(referenceView: self.view)
    let gravity = UIGravityBehavior(items: [view2])
    animator.addBehavior(gravity)
    let collosion = UICollisionBehavior(items: [view2])
    collosion.translatesReferenceBoundsIntoBoundary = true
    let dynamic = UIDynamicItemBehavior(items: [view2])
    dynamic.elasticity = 0.7
    animator.addBehavior(collosion)
    animator.addBehavior(dynamic)

这将以漂亮的弹跳效果放置 UIView。但是如何启动 UIView 呢?如何改变X和Y位置并保持添加的行为?

我想你需要的是UIPushBehavior。添加此额外行为。

let push = UIPushBehavior(items: [view2], mode: UIPushBehaviorMode.instantaneous)
push.setAngle(CGFloat(-M_PI/2 - 0.1), magnitude: 8.0) // Adjust angle and magnitude
animator.addBehavior(push)