iosswiftuikit-dynamicsuidynamicbehavioruidynamicanimator

UIFieldBehaviour for snapping dynamic view to top, bottom or middle of screen


I'm using a draggable view which is the width and height of the screen and can be moved up or down on the screen depending on the pan gesture.

When the pan gesture has ended I want it to snap to three different y positions, top, middle or bottom of the screen according to where the view was when the pan ended. I want something like the UIFieldBehaviour where a force is animating it to the correct position.

When in top the whole view will be displayed and when in the bottom only like 30px should be displayed and the rest of the view will be under the screen so to speak. Therefore I need the force to only behave to the top 30px of the view.

Force field behaviour (sorry for bad painting):

enter image description here

Snap behaviour:
enter image description here

Any ideas on how to achieve this?


Solution

  • It was easier than I thought. I'm using the new UIViewPropertyAnimator and when pan .ended I just check whether the targets y-position should snap to the top, bottom or middle by comparing it to the height of the UIScreen.

            animator!.addAnimations({
                print(target.frame.origin.y)
    
                if (target.frame.origin.y > UIScreen.main.bounds.height / 2) {
                    if (target.frame.origin.y > (UIScreen.main.bounds.height - 50)) {
                        target.frame = CGRect(x: 0, y: UIScreen.main.bounds.height - 25, width: self.view.frame.width, height: self.view.frame.height)
                    } else {
                    target.frame = CGRect(x: 0, y: UIScreen.main.bounds.height - 95, width: self.view.frame.width, height: self.view.frame.height)
                    }
                } else {
                    target.frame = CGRect(x: 0, y: 120, width: self.view.frame.width, height: self.view.frame.height)
                }
            })
            animator!.startAnimation()