Animated menu in swift UIKit - ios

i'm trying to present in my ViewController(which i will call main VC) and a MenuViewController . I've created MenuViewController with 1 view(which i've set like clear) and 2 views in it(1 is with tableView and button; second is bgcolor black with alpha 0.3).
PictureOfMenuViewController
What I'm trying to do:
to OVERLAY this menuVC on my ViewController with animation from right to left
I tried to do it (in my main VC) , like this :
#IBAction func menuButtonTapped(_ sender: Any) {
menuViewController = MenuViewController()
let transition = CATransition()
transition.duration = 1
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
view.window?.layer.add(transition, forKey: kCATransition)
self.present(menuViewController, animated: false)
but it PUSHES it instead of overlaying , and doing like screenshot of mainVC
My question is : how can i create an animation from right to left, is it possible , or like custom CATransitionType??

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.

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)

animation while changing view swift

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)
}

Can a UIView be fixed in place during a segue transition?

I am creating a board game where each board piece is an instance of a view controller. I have subclassed UIStoryboardSegue so that each piece smoothly flows from one to the other. However, my game piece stays centered in this frame during the transition. I would like to have it so that my gamePiece (a UIImageView) stays in the center of the screen throughout the entire transition, but haven't had any luck searching.
Here is my Segue subclass, if it helps at all:
import UIKit
import QuartzCore
class RightToLeftSegue: UIStoryboardSegue {
override func perform() {
let src: UIViewController = self.sourceViewController
let dst: UIViewController = self.destinationViewController
let transition: CATransition = CATransition()
let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
transition.duration = 1.25
transition.timingFunction = timeFunc
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
src.navigationController!.view.layer.addAnimation(transition, forKey: kCATransition)
src.navigationController!.pushViewController(dst, animated: true)
}
}
You can add this UIImageView to the window as subview before the transition happens.This way you will have it throughout the transition.
Let me know if this works out for you.

Resources