如何在iOS中使用Swift从右到左呈现视图控制器

时间:2023-01-23 23:44:02

I am using presentViewController to present new screen

我正在使用presentViewController来显示新的屏幕

let dashboardWorkout = DashboardWorkoutViewController()
presentViewController(dashboardWorkout, animated: true, completion: nil)

This presents new screen from bottom to top but I want it to presented from right to left without using UINavigationController.

这显示了从下到上的新屏幕,但我希望它在不使用UINavigationController的情况下从右到左显示。

I am using Xib instead of storyboard so how can I do that ?

我用的是Xib而不是storyboard那么我怎么做呢?

10 个解决方案

#1


94  

It doesn't matter if it is xib or storyboard that you are using. Normally, the right to left transition is used when you push a view controller into presentor's UINavigiationController.

不管你使用的是xib还是故事板。通常,当您将视图控制器推入presentor的UINavigiationController时,会使用从右到左的转换。

UPDATE

更新

Added timing function kCAMediaTimingFunctionEaseInEaseOut

添加定时功能kCAMediaTimingFunctionEaseInEaseOut

Sample project with Swift 4 implementation added to GitHub

在GitHub上添加了Swift 4实现的示例项目


Swift 3 & 4

斯威夫特3 & 4

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
present(dashboardWorkout, animated: false, completion: nil)


ObjC

ObjC

CATransition *transition = [[CATransition alloc] init];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.window.layer addAnimation:transition forKey:kCATransition];
[self presentViewController:dashboardWorkout animated:false completion:nil];


Swift 2.x

快2.倍

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
view.window!.layer.addAnimation(transition, forKey: kCATransition)
presentViewController(dashboardWorkout, animated: false, completion: nil)

Seems like the animated parameter in the presentViewController method doesn't really matter in this case of custom transition. It can be of any value, either true or false.

似乎presentViewController方法中的动画参数在自定义转换的情况下并不重要。它可以是任何值,或真或假。

#2


27  

Complete code for present/dismiss, Swift 3

完整的目前/解散代码,Swift 3

extension UIViewController {

    func presentDetail(_ viewControllerToPresent: UIViewController) {
        let transition = CATransition()
        transition.duration = 0.25
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromRight
        self.view.window!.layer.add(transition, forKey: kCATransition)

        present(viewControllerToPresent, animated: false)
    }

    func dismissDetail() {
        let transition = CATransition()
        transition.duration = 0.25
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromLeft
        self.view.window!.layer.add(transition, forKey: kCATransition)

        dismiss(animated: false)
    }
}

#3


12  

You can also use custom segue.

还可以使用自定义segue。

Swift 3

斯威夫特3

class SegueFromRight: UIStoryboardSegue
{ 
    override func perform()
    {
    let src = self.source
    let dst = self.destination

    src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
    dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)

    UIView.animate(withDuration: 0.25,
                   delay: 0.0,
                   options: UIViewAnimationOptions.curveEaseInOut,
                   animations: {
                    dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
    },
                   completion: { finished in
                    src.present(dst, animated: false, completion: nil)
    }
    )
}
}

#4


6  

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.

这里vc是视图控制器,在你的情况下。

#5


2  

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.

许多开发人员(像我)真的不喜欢使用NavigationController。通常情况下,以一种特别的方式呈现会更加灵活,特别是对于不寻常和复杂的应用程序。然而,添加一个导航控制器并不困难。

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

    在故事板上,点击进入VC并点击“在nav controller中嵌入->”。真的就是这样。或者,

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

    在didFinishLaunchingWithOptions中,如果你喜欢的话,很容易建立你的导航控制器

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

    实际上,您甚至不需要将变量保存在任何地方,因为. navigationcontroller始终作为一个属性可用—很简单。

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

一旦你有了一个navigationController,在屏幕之间进行转换是很简单的,

    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://*.com/a/48081504/294884 . Happy new year 2018!

幸运的是,在这个QA中有一些剪切和粘贴样板代码。https://*.com/a/48081504/294884。2018年新年快乐!

#6


1  

Try this.

试试这个。

let transition: CATransition = CATransition()
transition.duration = 0.3

transition.type = kCATransitionReveal
transition.subtype = kCATransitionFromLeft
self.view.window!.layer.addAnimation(transition, forKey: nil)
self.dismissViewControllerAnimated(false, completion: nil)

#7


1  

Read up all answers and can't see correct solution. The right way do to so is to make custom UIViewControllerAnimatedTransitioning for presented VC delegate.

通读所有的答案,看不出正确的答案。正确的方法是为所提供的VC委托定制uiviewcontrolleranimatedtransiatedtransi。

So it assumes to make more steps, but the result is more customizable and haven't some side effects, like moving from view together with presented view.

所以它假设要做更多的步骤,但是结果是更加可定制的,并且没有一些副作用,比如从视图移动到呈现的视图。

So, assume you have some ViewController, and there is a method for presenting

假设你有一个ViewController,有一个表示的方法

var presentTransition: UIViewControllerAnimatedTransitioning?
var dismissTransition: UIViewControllerAnimatedTransitioning?    

func showSettings(animated: Bool) {
    let vc = ... create new vc to present

    presentTransition = RightToLeftTransition()
    dismissTransition = LeftToRightTransition()

    vc.modalPresentationStyle = .custom
    vc.transitioningDelegate = self

    present(vc, animated: true, completion: { [weak self] in
        self?.presentTransition = nil
    })
}

presentTransition and dismissTransition is used for animating your view controllers. So you adopt your ViewController to UIViewControllerTransitioningDelegate:

presentTransition和dismis待遇用于动画您的视图控制器。所以你采用视图控制器到UIViewControllerTransitioningDelegate:

extension ViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return presentTransition
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return dismissTransition
    }
}

So the last step is to create your custom transition:

最后一步是创建自定义转换:

class RightToLeftTransition: NSObject, UIViewControllerAnimatedTransitioning {
    let duration: TimeInterval = 0.25

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView
        let toView = transitionContext.view(forKey: .to)!

        container.addSubview(toView)
        toView.frame.origin = CGPoint(x: toView.frame.width, y: 0)

        UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: {
            toView.frame.origin = CGPoint(x: 0, y: 0)
        }, completion: { _ in
            transitionContext.completeTransition(true)
        })
    }
}

class LeftToRightTransition: NSObject, UIViewControllerAnimatedTransitioning {
    let duration: TimeInterval = 0.25

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView
        let fromView = transitionContext.view(forKey: .from)!

        container.addSubview(fromView)
        fromView.frame.origin = .zero

        UIView.animate(withDuration: duration, delay: 0, options: .curveEaseIn, animations: {
            fromView.frame.origin = CGPoint(x: fromView.frame.width, y: 0)
        }, completion: { _ in
            fromView.removeFromSuperview()
            transitionContext.completeTransition(true)
        })
    }
}

In that code view controller is presented over current context, you can make your customizations from that point. Also you may see custom UIPresentationController is useful as well (pass in using UIViewControllerTransitioningDelegate)

在这个代码视图控制器中,您可以在当前上下文中进行定制。还可以看到定制的UIPresentationController也很有用(通过使用UIViewControllerTransitioningDelegate)

#8


0  

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.

我有一个更好的解决方案,如果您想保留苹果的经典动画,而没有看到使用CATransition()所看到的“黑色”转换,那么它对我很有用。简单地使用popToViewController方法。

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.

你可以通过搜索它的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.

要小心:例如,如果你正在浏览3个视图控制器,当到达最后一个你想要弹出第一个。在这种情况下,你将会失去对有效的第二视图控制器的引用,因为popToViewController已经覆盖了它。顺便说一句,有一个解决方案:你可以很容易地在popToViewcontroller之前保存,也就是你以后需要的VC。这对我很有效。

#9


0  

present view controller from right to left in iOS using Swift

目前的视图控制器从右到左在iOS中使用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]

具有:[https://developer.apple.com/documentation/quartzcore/catransition][1]

#10


-3  

This worked for me :

这对我很有效:

self.navigationController?.pushViewController(controller, animated: true)

#1


94  

It doesn't matter if it is xib or storyboard that you are using. Normally, the right to left transition is used when you push a view controller into presentor's UINavigiationController.

不管你使用的是xib还是故事板。通常,当您将视图控制器推入presentor的UINavigiationController时,会使用从右到左的转换。

UPDATE

更新

Added timing function kCAMediaTimingFunctionEaseInEaseOut

添加定时功能kCAMediaTimingFunctionEaseInEaseOut

Sample project with Swift 4 implementation added to GitHub

在GitHub上添加了Swift 4实现的示例项目


Swift 3 & 4

斯威夫特3 & 4

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
present(dashboardWorkout, animated: false, completion: nil)


ObjC

ObjC

CATransition *transition = [[CATransition alloc] init];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.window.layer addAnimation:transition forKey:kCATransition];
[self presentViewController:dashboardWorkout animated:false completion:nil];


Swift 2.x

快2.倍

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
view.window!.layer.addAnimation(transition, forKey: kCATransition)
presentViewController(dashboardWorkout, animated: false, completion: nil)

Seems like the animated parameter in the presentViewController method doesn't really matter in this case of custom transition. It can be of any value, either true or false.

似乎presentViewController方法中的动画参数在自定义转换的情况下并不重要。它可以是任何值,或真或假。

#2


27  

Complete code for present/dismiss, Swift 3

完整的目前/解散代码,Swift 3

extension UIViewController {

    func presentDetail(_ viewControllerToPresent: UIViewController) {
        let transition = CATransition()
        transition.duration = 0.25
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromRight
        self.view.window!.layer.add(transition, forKey: kCATransition)

        present(viewControllerToPresent, animated: false)
    }

    func dismissDetail() {
        let transition = CATransition()
        transition.duration = 0.25
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromLeft
        self.view.window!.layer.add(transition, forKey: kCATransition)

        dismiss(animated: false)
    }
}

#3


12  

You can also use custom segue.

还可以使用自定义segue。

Swift 3

斯威夫特3

class SegueFromRight: UIStoryboardSegue
{ 
    override func perform()
    {
    let src = self.source
    let dst = self.destination

    src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
    dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)

    UIView.animate(withDuration: 0.25,
                   delay: 0.0,
                   options: UIViewAnimationOptions.curveEaseInOut,
                   animations: {
                    dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
    },
                   completion: { finished in
                    src.present(dst, animated: false, completion: nil)
    }
    )
}
}

#4


6  

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.

这里vc是视图控制器,在你的情况下。

#5


2  

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.

许多开发人员(像我)真的不喜欢使用NavigationController。通常情况下,以一种特别的方式呈现会更加灵活,特别是对于不寻常和复杂的应用程序。然而,添加一个导航控制器并不困难。

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

    在故事板上,点击进入VC并点击“在nav controller中嵌入->”。真的就是这样。或者,

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

    在didFinishLaunchingWithOptions中,如果你喜欢的话,很容易建立你的导航控制器

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

    实际上,您甚至不需要将变量保存在任何地方,因为. navigationcontroller始终作为一个属性可用—很简单。

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

一旦你有了一个navigationController,在屏幕之间进行转换是很简单的,

    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://*.com/a/48081504/294884 . Happy new year 2018!

幸运的是,在这个QA中有一些剪切和粘贴样板代码。https://*.com/a/48081504/294884。2018年新年快乐!

#6


1  

Try this.

试试这个。

let transition: CATransition = CATransition()
transition.duration = 0.3

transition.type = kCATransitionReveal
transition.subtype = kCATransitionFromLeft
self.view.window!.layer.addAnimation(transition, forKey: nil)
self.dismissViewControllerAnimated(false, completion: nil)

#7


1  

Read up all answers and can't see correct solution. The right way do to so is to make custom UIViewControllerAnimatedTransitioning for presented VC delegate.

通读所有的答案,看不出正确的答案。正确的方法是为所提供的VC委托定制uiviewcontrolleranimatedtransiatedtransi。

So it assumes to make more steps, but the result is more customizable and haven't some side effects, like moving from view together with presented view.

所以它假设要做更多的步骤,但是结果是更加可定制的,并且没有一些副作用,比如从视图移动到呈现的视图。

So, assume you have some ViewController, and there is a method for presenting

假设你有一个ViewController,有一个表示的方法

var presentTransition: UIViewControllerAnimatedTransitioning?
var dismissTransition: UIViewControllerAnimatedTransitioning?    

func showSettings(animated: Bool) {
    let vc = ... create new vc to present

    presentTransition = RightToLeftTransition()
    dismissTransition = LeftToRightTransition()

    vc.modalPresentationStyle = .custom
    vc.transitioningDelegate = self

    present(vc, animated: true, completion: { [weak self] in
        self?.presentTransition = nil
    })
}

presentTransition and dismissTransition is used for animating your view controllers. So you adopt your ViewController to UIViewControllerTransitioningDelegate:

presentTransition和dismis待遇用于动画您的视图控制器。所以你采用视图控制器到UIViewControllerTransitioningDelegate:

extension ViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return presentTransition
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return dismissTransition
    }
}

So the last step is to create your custom transition:

最后一步是创建自定义转换:

class RightToLeftTransition: NSObject, UIViewControllerAnimatedTransitioning {
    let duration: TimeInterval = 0.25

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView
        let toView = transitionContext.view(forKey: .to)!

        container.addSubview(toView)
        toView.frame.origin = CGPoint(x: toView.frame.width, y: 0)

        UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: {
            toView.frame.origin = CGPoint(x: 0, y: 0)
        }, completion: { _ in
            transitionContext.completeTransition(true)
        })
    }
}

class LeftToRightTransition: NSObject, UIViewControllerAnimatedTransitioning {
    let duration: TimeInterval = 0.25

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView
        let fromView = transitionContext.view(forKey: .from)!

        container.addSubview(fromView)
        fromView.frame.origin = .zero

        UIView.animate(withDuration: duration, delay: 0, options: .curveEaseIn, animations: {
            fromView.frame.origin = CGPoint(x: fromView.frame.width, y: 0)
        }, completion: { _ in
            fromView.removeFromSuperview()
            transitionContext.completeTransition(true)
        })
    }
}

In that code view controller is presented over current context, you can make your customizations from that point. Also you may see custom UIPresentationController is useful as well (pass in using UIViewControllerTransitioningDelegate)

在这个代码视图控制器中,您可以在当前上下文中进行定制。还可以看到定制的UIPresentationController也很有用(通过使用UIViewControllerTransitioningDelegate)

#8


0  

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.

我有一个更好的解决方案,如果您想保留苹果的经典动画,而没有看到使用CATransition()所看到的“黑色”转换,那么它对我很有用。简单地使用popToViewController方法。

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.

你可以通过搜索它的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.

要小心:例如,如果你正在浏览3个视图控制器,当到达最后一个你想要弹出第一个。在这种情况下,你将会失去对有效的第二视图控制器的引用,因为popToViewController已经覆盖了它。顺便说一句,有一个解决方案:你可以很容易地在popToViewcontroller之前保存,也就是你以后需要的VC。这对我很有效。

#9


0  

present view controller from right to left in iOS using Swift

目前的视图控制器从右到左在iOS中使用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]

具有:[https://developer.apple.com/documentation/quartzcore/catransition][1]

#10


-3  

This worked for me :

这对我很有效:

self.navigationController?.pushViewController(controller, animated: true)