Dismiss View Controller with Custom Animation

Dismiss view controller with custom animation?


let transition: CATransition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.reveal
transition.subtype = CATransitionSubtype.fromRight
self.view.window!.layer.add(transition, forKey: nil)
self.dismiss(animated: false, completion: nil)

How to get a fade out animation while dismissing a view controller

Swift 4.2, iOS 11

Here is an elegant way using extensions:

extension CATransition {
func fadeTransition() -> CATransition {
let transition = CATransition()
transition.duration = 0.4
transition.type = CATransitionType.fade
transition.subtype = CATransitionSubtype.fromRight

return transition
}
}

Then you can just write these three lines of code in a function or inside the action button in your UIViewController:

private func dismissVC() {
let transition = CATransition().fadeTransition()
navigationController?.view.layer.add(transition, forKey: kCATransition)
navigationController?.popViewController(animated: false)
}

iOS Dismiss and Present view controller custom animation

Presenting a view controller using a custom animation:

CATransition* transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:reader animated:NO];

Dismissing a view controller using a custom animation:

CATransition* transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromLeft;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController popViewControllerAnimated:NO];

How to dismiss 2 modal view controllers without weird animation

Create a snapshot from the currently visible view and add it as a subview to the first presented view controller. To find that you can simply "loop through" the presenting view controllers and dismiss from the initial one:

@IBAction func dismissViewControllers(_ sender: UIButton) {
var initialPresentingViewController = self.presentingViewController
while let previousPresentingViewController = initialPresentingViewController?.presentingViewController {
initialPresentingViewController = previousPresentingViewController
}


if let snapshot = view.snapshotView(afterScreenUpdates: true) {
initialPresentingViewController?.presentedViewController?.view.addSubview(snapshot)
}

initialPresentingViewController?.dismiss(animated: true)
}

This is the result with slow animations enabled for the dismissal:
https://www.dropbox.com/s/tjkthftuo9kqhsg/result.mov?dl=0



Related Topics



Leave a reply



Submit