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
How to Create a Big, Red Uibutton with iOS
How to Change Uibutton Image in Swift
Pull to Refresh Uitableview Without Uitableviewcontroller
How to Use Presentmodalviewcontroller to Create a Transparent View
Getting Time Elapsed in Objective-C
Reasons for Rejecting iPhone Application by Apple Store
Uiview - How to Get Notified When the View Is Loaded
How to Create a New Swift Project Without Using Storyboards
What Is a "Delegate" in Objective C's iPhone Development
Uibezierpath Triangle with Rounded Edges
How to Change the Tint Color of the Clear Button on a Uitextfield
Pixel Array to Uiimage in Swift
How to Prevent the iPhone Screen from Dimming or Turning Off While My Application Is Running
Cannot Subscript a Value of Type '[Nsobject:Anyobject]' with an Index of Type 'String'
This Certificate Has an Invalid Issuer:Keychain Marks All Certificates as "Invalid Issuer"