How to present view controller from right to left in iOS using Swift

前端 未结 12 1185
悲&欢浪女
悲&欢浪女 2020-12-04 06:03

I am using presentViewController to present new screen

let dashboardWorkout = DashboardWorkoutViewController()
presentViewController(dashboardWorkout, anima         


        
相关标签:
12条回答
  • 2020-12-04 06:38

    I have a better solution that has worked for me if you want to mantain the classic animation of Apple, without see that "black" transition that you see using CATransition(). Simply use popToViewController method.

    You can look for the viewController you need in

    self.navigationController.viewControllers // Array of VC that contains all VC of current stack
    

    You can look for the viewController you need by searching for it's restorationIdentifier.

    BE CAREFUL: for example, if you are navigating through 3 view controllers, and when arrive to the last you want to pop the first. You will lose, in this case, the refer to the effective SECOND view controller because the popToViewController has overwritten it. BTW, there's a solution: you can easily save before the popToViewcontroller, the VC you will need later. It worked for me very well.

    0 讨论(0)
  • 2020-12-04 06:39

    Try this,

        let animation = CATransition()
        animation.duration = 0.5
        animation.type = kCATransitionPush
        animation.subtype = kCATransitionFromRight
         animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        vc.view.layer.addAnimation(animation, forKey: "SwitchToView")
    
        self.presentViewController(vc, animated: false, completion: nil)
    

    Here vc is viewcontroller, dashboardWorkout in your case.

    0 讨论(0)
  • 2020-12-04 06:40
    let transition = CATransition()
        transition.duration = 0.25
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromLeft
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        tabBarController?.view.layer.add(transition, forKey: kCATransition)
        self.navigationController?.popToRootViewController(animated: true)
    
    0 讨论(0)
  • 2020-12-04 06:41

    This worked for me :

    self.navigationController?.pushViewController(controller, animated: true)
    
    0 讨论(0)
  • 2020-12-04 06:43

    If you do want to use the "quick fix" CATransition method....

    class AA: UIViewController
    
     func goToBB {
    
        let bb = .. instantiateViewcontroller, storyboard etc .. as! AlreadyOnboardLogin
    
        let tr = CATransition()
        tr.duration = 0.25
        tr.type = kCATransitionMoveIn // use "MoveIn" here
        tr.subtype = kCATransitionFromRight
        view.window!.layer.add(tr, forKey: kCATransition)
    
        present(bb, animated: false)
        bb.delegate, etc = set any other needed values
    }
    

    and then ...

    func dismissingBB() {
    
        let tr = CATransition()
        tr.duration = 0.25
        tr.type = kCATransitionReveal // use "Reveal" here
        tr.subtype = kCATransitionFromLeft
        view.window!.layer.add(tr, forKey: kCATransition)
    
        dismiss(self) .. or dismiss(bb), or whatever
    }
    

    All of this is unfortunately not really correct :(

    CATransition is not really made for doing this job.

    Note you will get the annoying cross fade to black which unfortunately ruins the effect.


    Many devs (like me) really don't like using NavigationController. Often, it is more flexible to just present in an ad-hoc manner as you go, particularly for unusual and complex apps. However, it's not difficult to "add" a nav controller.

    1. simply on storyboard, go to the entry VC and click "embed -> in nav controller". Really that's it. Or,

    2. in didFinishLaunchingWithOptions it's easy to build your nav controller if you prefer

    3. you really don't even need to keep the variable anywhere, as .navigationController is always available as a property - easy.

    Really, once you have a navigationController, it's trivial to do transitions between screens,

        let nextScreen = instantiateViewController etc as! NextScreen
        navigationController?
            .pushViewController(nextScreen, animated: true)
    

    and you can pop.

    That however only gives you the standard apple "dual push" effect...

    (The old one slides off at a lower speed, as the new one slides on.)

    Generally and surprisingly, you usually have to make the effort to do a full custom transition.

    Even if you just want the simplest, most common, move-over/move-off transition, you do have to do a full custom transition.

    Fortunately, to do that there's some cut and paste boilerplate code on this QA... https://stackoverflow.com/a/48081504/294884 . Happy new year 2018!

    0 讨论(0)
  • 2020-12-04 06:53

    present view controller from right to left in iOS using Swift

    func FrkTransition() 
    
    {
        let transition = CATransition()
    
        transition.duration = 2
    
        transition.type = kCATransitionPush
    
        transitioningLayer.add(transition,forKey: "transition")
    
        // Transition to "blue" state
    
        transitioningLayer.backgroundColor = UIColor.blue.cgColor
        transitioningLayer.string = "Blue"
    }
    

    Refrence By : [https://developer.apple.com/documentation/quartzcore/catransition][1]

    0 讨论(0)
提交回复
热议问题