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
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
I had referred to this question and several question about view / view controller transition but still couldn't find an satisfied answer. Most of the solutions suggest to flip the views instead of view controllers. However, the two view controllers in my app have totally different operation and implementation logic, thus I'm avoiding to mix them together.
In my app, I have a modal view controller FrontViewController which is embedded in a NavigationController. After pushing one button on the view, the modal view controller should flip to BackViewController, and vice versa. I had ever tried the following in FrontViewController:
let navi = UINavigationController(rootViewController: backController)
navi.modalPresentationStyle = .CurrentContext
navi.modalTransitionStyle = .FlipHorizontal
self.presentViewController(backController, animated: true, completion: nil)
This works almost as what I want, except that it flips navigation bar as well. Moreover, if I dismiss the modal view, only the view controller on the top of the stack is dismissed, while I failed to get the right parent/presenting controller to dismiss all the other stack controllers in the modal view.
Thus I further tried to prevent viewcontroller stack and use transitionFromViewController in FrontViewController using the same navigation controller instead:
self.navigationController!.addChildViewController(backController)
self.willMoveToParentViewController(nil)
self.navigationController!.transitionFromViewController(self, toViewController: backViewController, duration: 1, options: .TransitionFlipFromLeft, animations: {}, completion: ({Bool -> Void in
self.removeFromParentController()
c.didMoveToParentViewController(self)
}))
Then I got this run time error on executiing: Parent view controller is using legacy containment in call to -[UIViewController transitionFromViewController:toViewController: duration:options:animations:completion:]
So, how to transition between two view controllers while preventing them remain in the view controller stack?
You can add custom transition to the navigation controllers layer just before pushing the view controller.
let transition = CATransition()
transition.duration = 0.3
transition.type = "flip"
transition.subtype = kCATransitionFromLeft
self.navigationController?.view.layer.addAnimation(transition, forKey: kCATransition)
self.navigationController?.pushViewController(viewController!, animated: false)
Note that the animated parameter should be false. Otherwise the default sliding animation will takes place
See this demo for you :
Swift code for flipAnimation :
let mainStory = UIStoryboard(name: "Main", bundle: nil)
let search = mainStory.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
UIView.beginAnimations("animation", context: nil)
UIView.setAnimationDuration(1.0)
self.navigationController!.pushViewController(search, animated: false)
UIView.setAnimationTransition(UIViewAnimationTransition.FlipFromLeft, forView: self.navigationController!.view, cache: false)
UIView.commitAnimations()
FlipViewController Animation
Output :
Swift 5 - I personally like using this approach.
//PUSH
let secondVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "secondVC") as! SecondVC
self.navigationController?.pushViewController(secondVC, animated: false)
UIView.transition(from: self.view, to: secondVC.view, duration: 0.85, options: [.transitionFlipFromLeft])
//POP
let firstVC = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count ?? 2) - 2] as? FirstVC
if let firstView = firstVC?.view{
self.navigationController?.popViewController(animated: false)
UIView.transition(from: self.view, to: firstView, duration: 0.85, options: [.transitionFlipFromRight])
} else {
self.navigationController?.popViewController(animated: true)
}
Swift 3.0 +
Should use UIView's animation block, make sure your viewController in UINavigationController stacks:
let viewController = ViewController(nibName: "xxx", bundle: nil)
UIView.transition(with: self.navigationController!.view, duration: 1.0, options: .transitionFlipFromLeft, animations: {
self.navigationController?.pushViewController(viewController, animated: false)
}, completion: nil)
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
// for back button
changeTransition()
}
//btnMap.addTarget(self, action: #selector(searchHotelsResultVC.goToMap), for: .touchUpInside)
//btnMap.addTarget(self, action: #selector(MapViewController.backToList), for: .touchUpInside)
func goToMap() {
// for pushing
changeTransition()
navigationController?.pushViewController(settingsVC, animated: false)
}
func backToList() {
// for dismiss
changeTransition()
navigationController?.popViewController(animated: false)
dismiss(animated: true, completion: nil)
}
func changeTransition() {
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
//transition.type = kCATransitionPush
transition.type = "flip"
transition.subtype = kCATransitionFromLeft
navigationController?.view.layer.add(transition, forKey: kCATransition)
}
here Student is NSObjectClass
var Arraydata:[Student] = []
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let arr = Arraydata[indexPath.row]
if indexPath.row == arr
{
let story = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
UIView.beginAnimations("", context: nil)
UIView.setAnimationDuration(1.0)
UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)
UIView.setAnimationTransition(UIViewAnimationTransition.flipFromRight, for: (self.navigationController?.view)!, cache: false)
self.navigationController?.pushViewController(story, animated: true)
UIView.commitAnimations()
}
}
I'm trying to show view controller which is 2nd view controller in a navigation view controller. What I want to show RegistraionViewController if join is tapped. Based on that. I push registrationViewController to the navigation Controller where LoginViewController is rootView Controller. Now the issue is when Join is Tapped. It Should show registrationView Controller but it shows Login screen and after a delay of about half second. It shows registration screen. My Code is as follows:
if let navC = self.selectedViewController as? UINavigationController
{
if let vc = navC.viewControllers.first as? LoginViewController
{
if let registerVC = self.storyboard?.instantiateViewControllerWithIdentifier("RegistrationViewControllerIdentifier") as? RegistrationViewController
{
var view = navC.view
vc.navigationController?.pushViewController(registerVC, animated: false)
}
}
}
I have also tried by changing the transition of views and giving animation time as 0. But no luck. Which is :
if let registerVC = self.storyboard?.instantiateViewControllerWithIdentifier("RegistrationViewControllerIdentifier") as? RegistrationViewController
{
UIView.animateWithDuration(0.0, animations: {
if let view = self.navigationController?.view
{
self.navigationController?.pushViewController(registerVC, animated: false)
UIView.setAnimationTransition(UIViewAnimationTransition.None, forView:view , cache: false)
}
}, completion: {
(value: Bool) in
})
}
I explain my problem.
I just made a game with 3 views.
the game presentation with a playbutton
the game scene
the game over scene with a button to play again or go back to the presentation scene.
My problem is when there is a transition between two scenes, the active memory will be 30MB more and each scene transition will increase the active memory about 30MB and will never decrease.
How can I fix it and release memory ?
Thank you all
My transition code :
// Transition in presentationViewController file
func transition(sender:UIButton!)
{
println("transition")
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("GameViewController") as UIViewController
let window = UIApplication.sharedApplication().windows[0] as UIWindow
UIView.transitionFromView(
window.rootViewController!.view,
toView: secondViewController.view,
duration: 0.65,
options: .TransitionCrossDissolve,
completion: {
finished in window.rootViewController = secondViewController
})
}
// transition in GameScene file
func removeCountDownTimerView()
{
defaults.setInteger(balloonDestroyed, forKey: "score")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let gameOverScene: UIViewController = storyboard.instantiateViewControllerWithIdentifier("GameOverViewController") as UIViewController
let vc = self.view?.window?.rootViewController
vc?.presentViewController(gameOverScene, animated: true, completion: nil)
}
// transition in gameOverViewController file
func transition(sender:UIButton!)
{
println("play transition")
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("GameViewController") as UIViewController
let window = UIApplication.sharedApplication().windows[0] as UIWindow
UIView.transitionFromView(
window.rootViewController!.view,
toView: secondViewController.view,
duration: 0.65,
options: .TransitionCrossDissolve,
completion: {
finished in window.rootViewController = secondViewController
})
}
instantiateViewControllerWithIdentifier("GameViewController") as UIViewController
This code is creating new ViewController each time user press the button. I advise you to use singletone pattern for this.
private let _SomeManagerSharedInstance = GameViewController()
class GameOverViewController {
class var sharedInstance: GameOverViewController {
return _GameOverViewController
}
You can also create a private class method for instantiating the View Controller from storyboard.