简体   繁体   English

“启动”并删除UIView

[英]“Launch” and drop a UIView

在此输入图像描述

Above picture illustrates what I want: 上图说明了我想要的东西:

  • The red circle is the starting point of a UIView 红圈是UIView的起点
  • I want to launch it upwards (Y position) with a change in the X position 我希望通过改变X位置向上启动它(Y位置)
  • I want to make it drop within boundaries. 我想让它落入边界内。

What I tried: 我尝试了什么:

For the launch I can make use of the UIView.animate function. 对于启动,我可以使用UIView.animate函数。 For the drop I can use the UIDynamicAnimator. 对于drop,我可以使用UIDynamicAnimator。 However there are some problems: 但是有一些问题:

-In the UIView.animate I cannot 'curve' the animation, only a straight line. - 在UIView.animate中,我不能'曲线'动画,只有一条直线。 I can use this answer here to draw a curve line: https://stackoverflow.com/a/43813458/7715250 我可以在这里使用这个答案画一条曲线: https//stackoverflow.com/a/43813458/7715250

-Combining both functions is not working. - 两个功能的组合都不起作用。 After the UIView.animate is done, the UIView just drop straight downwards. UIView.animate完成后,UIView直接向下移动。

The code for UIDynamicAnimator: 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)

That will drop the UIView with a nice bounce effect. 这将使UIView有一个很好的反弹效果。 But how to launch the UIView? 但是如何推出UIView? How to change the X and Y position and remain the added behaviours? 如何更改X和Y位置并保持添加的行为?

I think what you need is UIPushBehavior. 我认为你需要的是UIPushBehavior。 Add this extra behavior. 添加这个额外的行为。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM