How to do custom presentation with UIViewPropertyAnimator in iOS

Issue #337

Normally we just present from any UIViewController in any UINavigationController in UITabBarController and it will present over tabbar

1
present(detailViewController, animated: true, completion: nil)

If we have animation with UIViewPropertyAnimator, then we can implement UIViewControllerAnimatedTransitioning and interruptibleAnimator(using:)

The methods in this protocol let you define an animator object, which creates the animations for transitioning a view controller on or off screen in a fixed amount of time. The animations you create using this protocol must not be interactive. To create interactive transitions, you must combine your animator object with another object that controls the timing of your animations.

Implement this method when you want to perform your transitions using an interruptible animator object, such as a UIViewPropertyAnimator object. You must return the same animator object for the duration of the transition.

For more fine-grained control, we can have UIPresentationController

From the time a view controller is presented until the time it is dismissed, UIKit uses a presentation controller to manage various aspects of the presentation process for that view controller. The presentation controller can add its own animations on top of those provided by animator objects, it can respond to size changes, and it can manage other aspects of how the view controller is presented onscreen.

A lazy approach is to present without animation and do animation after

1
2
3
4
present(detailViewController, animated: false, completion: {
let animator = UIViewPropertyAnimator()
animator.startAnimation()
})

If we don’t want to involve UIViewController then we can work on UIView level. This way we can animate hiding tab bar. Any UIViewController within UITabBarController has tabBarController

1
2
3
4
5
let animator = UIViewPropertyAnimator()
animator.addAnimations {
self.tabBarController?.tabBar.transform = CGAffineTransform(translationX: 0, y: tabbar.frame.height)
}
animator.startAnimation()

Comments