简体   繁体   中英

Pop UIViewController with custom “dismiss modal”-like animation

I'm trying to customise the transition animations of a UINavigationController push/pop based navigation in a way that simulates presenting/dismissing UIViewController s.

Here's an example of the standard animations:

  • push to Green screen,
  • present modal Orange screen,
  • dismiss modal Orange screen and
  • pop back to Cyan screen.

在此处输入图片说明

I was able to obtain the same "slide up from the bottom" push animation implementing a custom vertical UIStoryboardSegue .

The hard part is to implement the counterpart pop animation. The best I'm able to get is the following:

在此处输入图片说明

Here's a slow motion version of the same effect:

在此处输入图片说明

From the previous animation you can appreciate that it's different from the standard dismiss modal animation mainly because the Bubblegum screen shouldn't slide from top to bottom, but should be already present behind the Navy screen during the pop.

This is the code I've used to create the fake dismiss modal animation:

class FakeModalNavigationController: UINavigationController {

    fileprivate static let unwindToBubblegumScreenSegueID = "unwindToBubblegumScreenSegueID"

    override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) {
        if unwindSegue.identifier == type(of: self).unwindToBubblegumScreenSegueID {
            popViewControllerAnimatedFromBottom(subsequentVC)
        }
    }

    fileprivate func popViewControllerAnimatedFromBottom(_ viewControllerToPop: UIViewController) {
        let transition = CATransition()
        transition.duration = 0.25
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromBottom
        view.layer.add(transition, forKey: nil)
        popViewController(animated: false)
    }
}

Thanks in advance for answers and comments!

Try to use:

transition.type = kCATransitionReveal
transition.subtype = kCATransitionFromBottom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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