animation while changing view swift - ios

I am new to swift.I want to do animation while changing view from one view-controller to other view-controller.i am using prepareForSegue: method on button click, i have searched lot but did not find any solution do anyone have solution for this?

// call following code in the method where you want to add animation effect.
func buttonClicked(sender: UIButton){
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.navigationController?.view.layer.addAnimation(transition, forKey: nil)
self.navigationController?.pushViewController(vc!, animated: true)
}
// you can vary the duration of transition according to your convenience
// here transition.type are types of animation which you want to add on view. now we are pushing view so used push type. if want, you can google it for more types.
// subtype will decide the direction on animation.

You do not have to use prepareForSeque, you can do like this:
#IBAction func buttonClicked(sender: UIButton) {
let destinationVC = storyboard?.instantiateViewControllerWithIdentifier("viewcontrollerID") as! ViewControllerClass
self.presentViewController(destinationVC, animated: true, completion: nil)
}
viewcontrollerID is set in storyboard (it is called identity in attribute inspector (right panel)), it should be unique, ViewControllerClass is class of your ViewController
If you are using UINavigationController
#IBAction func buttonClicked(sender: UIButton) {
let destinationVC = storyboard?.instantiateViewControllerWithIdentifier("viewcontrollerID") as! ViewControllerClass
self.navigationController?.pushViewController(destinationVC, animated: true)
}

Related

How to achieve plain standard slide-from-right transition without UINavigationController?

To me the transition from right to left while opening new view is the most essential part of iPhone UI. How can it be that nearly in 2020 there is no way to have it for transition between two ViewControllers? At least I could not find the way.
Due to reasons I am not using NavigationController in the app. Instead I just call self.present(newVC) to open new one and self.dismiss() to go back.
I just want the new ViewController to slide from the right, that's all.
Any hints?
It seems you want to slide from right to left.
So you can do it using a custom segue transition.
First Create CATransition extenstion than add layer to your segue .
func rightToLeft() -> CATransition {
self.duration = 0.1 //set the duration to whatever you'd like.
self.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
self.type = kCATransitionReveal
self.subtype = kCATransitionFromRight
return self
}
}
Implementation :
DispatchQueue.main.async { //make sure all UI updates are on the main thread.
nav?.view.layer.add(CATransition().rightToLeft(), forKey: nil)
nav?.pushViewController(YourViewController(), animated: false)
}
So you want just slide from right to left here is code :
let transition = CATransition()
transition.duration = 0.3
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
let presentedVC = self.storyboard!.instantiateViewController(withIdentifier: "PresentingViewController") \\ view Controller identifire in storyboard
presentedVC.view.backgroundColor = UIColor.orange
present(presentedVC, animated: false, completion: nil)
Here is Sample project

View Controller turning up black even though there is content

So when I click on a button from another View Controller, it is suppose to present the next View Controller. However, when clicking the button, it just show the next View Controller with a black screen, even though there is content on it. How do I fix this?
I do know that there had already been similar questions asked but none helped me in solving this issue.
Button clicked code:
#IBAction func onSignUp(_ sender: Any) {
presentDetail(SignUpViewController())
}
Present next View Controller and animation code:
extension UIViewController {
func presentDetail(_ viewControllerToPresent: UIViewController) {
let transition = CATransition()
transition.duration = 0.25
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
self.view.window!.layer.add(transition, forKey: kCATransition)
self.present(viewControllerToPresent, animated: false)
}
func dismissDetail() {
let transition = CATransition()
transition.duration = 0.25
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromLeft
self.view.window!.layer.add(transition, forKey: kCATransition)
dismiss(animated: false)
}
}
To clarify, the presentDetail function suppose to present the next View Controller with an animation (From Right), as shown in the code.
You need to instantiate the target ViewController from Storyboard before presenting it.
#IBAction func onSignUp(_ sender: Any) {
presentDetail(
storyboard?.instantiateViewController(withIdentifier: "putyourIdentifierhere"))
}
instantiateViewController function returns the Viewcontroller you want to present.

Same controller loads twice before segue (programatically) to second VC in Swift?

I have come across similar issues but nothing like my current setup and thus struggling to find a fix.
I have a custom UIViewController extension with two functions which programmatically presents and dismisses a VC (see below).
extension UIViewController {
//present VC fromRight
func presentDetail(_ viewControllerToPresent: UIViewController){
//constant to hold animation
let transition = CATransition()
transition.duration = 0.3 //seconds
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.view.window?.layer.add(transition, forKey: kCATransition)
present(viewControllerToPresent, animated: false, completion: nil)
}//end func
//dismiss VC fromLeft
func dismissDetail(){
//constant to hold animation
let transition = CATransition()
transition.duration = 0.3 //seconds
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
self.view.window?.layer.add(transition, forKey: kCATransition)
dismiss(animated: false, completion: nil)
}//end func
}
I have a VenueListVC (primary VC) that has a UITableView, my didSelectRowAt function is below which presents (using the custom extension above) a VenueDetailsVC (secondary VC).
My issue is that when a cell is selected the primary VC (VenueListVC) is loaded twice before modally presenting the secondary VC (VenueDetailsVC). I have no other segues between these two VCs.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let venueDetailsVC = storyboard?.instantiateViewController(withIdentifier: "VenueDetailsVC") as? VenueDetailsVC else { return } //create an instance of VenueDetailsVC
presentDetail(venueDetailsVC)//present Group Feed VC using custom extension
}

how to change navigation animation to left to right in swift 4?

I have a navigation bar in my application that I change the navigation to present modally But I want when user goes to the destination view controller the navigation animation from left to right
here is my codes in the first view controller
#IBAction func showProfilePage(_ sender: UIBarButtonItem) {
self.performSegue(withIdentifier: "showprofile", sender: self)
}
I read apple site for this and use this function too
private func navigationController(_ navigationController: UINavigationController,
willShow viewController: ProfileViewController,
animated: Bool) {
}
But it doesn't work so what should I do to change navigation animation from left to right?
Try this.
let storyBoard : UIStoryboard = UIStoryboard(name: "PatientCheckout", bundle:nil)
let controller = storyBoard.instantiateViewController(withIdentifier: "PatientCheckoutViewController") as UIViewController
let transition = CATransition.init()
transition.duration = 0.45
transition.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionDefault)
transition.type = kCATransitionPush //Transition you want like Push, Reveal
transition.subtype = kCATransitionFromLeft // Direction like Left to Right, Right to Left
transition.delegate = self
view.window!.layer.add(transition, forKey: kCATransition)
self.navigationController?.pushViewController(controller, animated: true)

iOS presenting view controller animated as 'Push' (right-left animation)

Currently, I have a view controller presenting other view controller. What I'm trying to do is to recreate the default animation used when pushing a view controller.
My current approach is:
FirstViewController:
#IBAction private func push(sender: AnyObject) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SecondViewController")
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window?.layer.addAnimation(transition, forKey: kCATransition)
presentViewController(vc, animated: false, completion: nil)
}
SecondViewController:
#IBAction private func pop(sender: AnyObject) {
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
view.window?.layer.addAnimation(transition, forKey: kCATransition)
dismissViewControllerAnimated(false, completion: nil)
}
It is almost working, but I have a weird behaviour, I'm having a kind of black screen/flash when transitioning between view controllers. I already tried changing window.backgroundColor but it is not fixing the issue.
Thanks in advance 0_0...
The trouble is merely that what you're doing is not how to customize the animation for a present/dismiss transition. Apple has provided you with a clear, well-establish, official way to do that, and what you're doing is not it. You need to give your presented view controller a transitioningDelegate along with implementations of animationControllerForPresentedController: and animationControllerForDismissedController:, and implement the UIViewControllerAnimatedTransitioning protocol, possibly along with a custom UIPresentationController subclass.

Resources