
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?
source share