Slide from right animation when changing rootViewController - ios

I'm trying to change my rootViewController with a slide-from-right animation.
I know how to do it with a flip animation:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("LandingViewController")
UIView.transitionWithView( UIApplication.sharedApplication().keyWindow!,
duration: 0.5,
options: UIViewAnimationOptions.TransitionFlipFromLeft,
animations: { UIApplication.sharedApplication().keyWindow?.rootViewController = viewController; },
completion: nil)
However, UIViewAnimationOptions does NOT have a slide-from-right animation.
How can I achieve this?

Related

How to transition from viewcontroller to TabbarController Programmatically without StoryBoards?

From my onboarding viewcontroller i need to transition to my tabBarController which is also a navigationController and i want to make it as a root viewcontroller afterwards.
#objc func willGoToMain(sender: UIButton!) {
let tabBarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabBarController)
navigationController.isNavigationBarHidden = true
self.present(tabBarController, animated: true, completion: nil)
}
Thread 1: "Application tried to present modally a view controller <MyStarterProject.TabBarController: 0x7f9bd8011400> that has a parent view controller <UINavigationController: 0x7f9bd7022800>
I like to transition it like a modal presentation or cross dissolve. not just to appeared as a rootviewcontroller all of a sudden.
Here's what i'm trying to do and this solves my problem.
#objc func willGoToMain(sender: UIButton!) {
guard let window = UIApplication.shared.keyWindow else {
return
}
let tabbarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabbarController)
navigationController.isNavigationBarHidden = true
window.rootViewController = navigationController
window.makeKeyAndVisible()
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.3
UIView.transition(with: window, duration: duration, options: options, animations: {}, completion:
{ completed in
// maybe do something on completion here
})
}
Change this:
self.present(tabBarController, animated: true, completion: nil)
with this:
self.present(navigationController, animated: true, completion: nil)
Please try this one.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = storyboard.instantiateViewController(identifier: "MainTabBarController")
mainTabBarController.modalPresentationStyle = .fullScreen
self.present(mainTabBarController, animated: true, completion: nil)
If you need more details. please visit this blog
https://fluffy.es/how-to-transition-from-login-screen-to-tab-bar-controller/

How to present (popover) view controller without previous view controller moving back (stop weird animation)

When you present (popover) a view controller the previous view controller kind of backs out of the screen. Is there any way to stop that from happenening.
Here is an example of what is happening: https://imgur.com/a/DSKC6vF
And here is an example of what I want to happen: https://imgur.com/a/vyjA1Jv
The only code I'm using for presenting it
let viewController = UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewController(identifier: "IdentifierName")
self.present(viewController, animated: true, completion: nil)
If you want something like this:
First ViewController:
let sb = UIStoryboard.init(name: "Appointment", bundle: nil)
let popVC = sb.instantiateViewController(withIdentifier: "AppointConfirmationPopUpVC") as! AppointConfirmationPopUpVC
self.addChild(popVC)
popVC.view.frame = self.view.frame
self.view.addSubview(popVC.view)
popVC.didMove(toParent: self)
Second ViewController:
#IBOutlet weak var bottomView: UIView!
viewdidLoad()
{
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
showAnimate()
}
func showAnimate(){
UIView.animate(withDuration: 0.2, delay: 0, options: [.curveEaseIn],
animations: {
self.bottomView.center.y -= self.bottomView.bounds.height
self.bottomView.layoutIfNeeded()
}, completion: nil)
}
func hideAnimate()
{
self.tabBarController?.tabBar.isHidden = false
UIView.animate(withDuration: 0.2, delay: 0, options: [.curveLinear],
animations: {
self.bottomView.center.y += self.bottomView.bounds.height
self.bottomView.layoutIfNeeded()
self.view.center.y += self.view.bounds.height
self.view.layoutIfNeeded()
}, completion: {(_ completed: Bool) -> Void in
})
}

Transition Animation is not working Swift 5

I am trying to navigate from one view controller to other using tap gesture navigation. I want add transition animation into it. I tried with two ways but it didn't worked for me. -
First approach :
UIView.animate(withDuration: 0.5) { ()-> Void in
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let myTabBarController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
var appDel = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = myTabBarController
self.view.layoutIfNeeded()
}
Second approach :
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: [.curveEaseIn],
animations: {
let controller = self.storyboard!.instantiateViewController(withIdentifier: "mainMenuViewController") as! MainMenuViewController
self.addChild(controller)
self.view.addSubview(controller.view)
controller.didMove(toParent: self)
}, completion: nil)
Can anybody please tell me whats the solution ?
try this to present
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
self.present(nextViewController, animated:true, completion:nil)
If you present a ViewController then you can only Dismiss it, It can be done by below code
self.dismiss(animated: true, completion: nil)
To push
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
self.navigationController?.pushViewController(nextViewController, animated:true)
If you push a ViewController then you can only Pop it, It can be done by below code
self.navigationController?.popViewController(animated: true)

Present New ViewController programmatically [Swift 3]

What I am trying to do is present a new view controller programmatically with Swift 3 with an animated fade-out then fade-in and a pop-up then pop-down depending on what view controllers the user enters. This is so my app feels more modern and less old and blocky.
Here is the current method I am using for presenting a new view controller. It works, but it is rather abrupt and robotic:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let ExampleVC = storyBoard.instantiateViewController(withIdentifier: "ExampleVC")
//ExampleVC standing for ExampleViewController
self.navigationController?.pushViewController(ExampleVC, animated: false)
here is a method for animating UIViews but it doesnt work with UIViewControllers
func popIn(yourView : UIView) {
yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: {
yourView.transform = .identity
}, completion: nil)
self.view.addSubview(yourView)
}
i need something similar to this but for a UIViewController
UPDATE
here is what you would use to to present a new viewcontroller in a cool way to make your app more modern
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let VC = storyboard.instantiateViewController(withIdentifier: "Example") //<<< make sure you enter in your storyboard ID here or you will crash your app
VC.modalTransitionStyle = .crossDissolve //you can change this to do different animations
VC.view.layer.speed = 0.1 //adjust this to animate at different speeds
self.navigationController?.present(VC, animated: true, completion: nil)
if you have any questions comment them so i can help you guys out
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var ivc = storyboard.instantiateViewController(withIdentifier: "ExampleVC")
ivc.modalTransitionStyle = .crossDissolve
ivc.view.layer.speed = 0.1
self.present(ivc, animated: true, completion: { _ in })
Check this code for animation to view controller

Custom Animations while changing RootViewcontroller

on the very first launch of my app i wanna show a Specific View Controller say firstLaunchVC and for that i'm doing this
in my didFinishLaunchingWithOptions
if (itsFirstLaunch){
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SignupViewController") as! SignupViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
its working fine but now after completing my signup i want to change my RootViewController to my default ViewController say DefaultVC
appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("DefaultVC") as! UITabBarController
UIView.transitionWithView(self.appDelegate.window!, duration: 0.3, options: .TransitionFlipFromBottom, animations: {() -> Void in
self.appDelegate.window?.rootViewController = initialViewController
self.appDelegate.window?.makeKeyAndVisible()
}, completion: { _ in }
)
its working fine and you can also see that i added animation in this Transition
UIView.transitionWithView(self.appDelegate.window!, duration: 0.3, options: .TransitionFlipFromBottom, animations: {()....
but this animation is not good enough also here we have a lot of options like :
TransitionFlipFromLeft
TransitionFlipFromRight
TransitionCurlUp
TransitionCurlDown
TransitionCrossDissolve
TransitionFlipFromTop
TransitionFlipFromBottom etc...
but none of them are meeting my requirements (the are totally hideous) what i want is the Animation when a ViewController being dismissed i want to slide out the vc from the frame in a direction if anybody can guide me how to do this then it'll be so helpful for me

Resources