Run and uninstall UIView

enter image description here

The figure above shows what I want:

  • The red circle is the starting point of the UIView
  • I want to run it up (position Y) with changing position X
  • I want him to fall within the borders.

What I tried:

To start, I can use the UIView.animate function. For the drop, I can use the UIDynamicAnimator. However, there are some problems:

- In UIView.animate I can’t β€œtwist” the animation, only the direct one. I can use this answer here to draw a curve line: stack overflow

-The combination of both functions does not work. After executing UIView.animate, the UIView will simply go down.

Code for 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) 

This will reduce the UIView with a good bounce effect. But how to start UIView? How to reposition X and Y and remain an added behavior?

+1
source share
1 answer

I think you need 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) 
+3
source

Source: https://habr.com/ru/post/1272370/


All Articles