将动画从一个视图控制器过渡到另一个视图控制器

时间:2018-12-29 11:02:43

标签: ios objective-c uiviewcontroller uiviewanimationtransition

我有2个带有导航控制器的视图控制器。我将一个firstViewController推送到secondViewController。我目前正在使用

[self.navigationController pushViewController:detailsViewController animated:YES];

但是过渡是secondView从右边进来。我想做的是将secondView从背面进来,并且firstView淡出,而secondView淡入。

反正有实现这一目标的方法吗?

1 个答案:

答案 0 :(得分:0)

在Swift中推送/弹出UIVIewController FadeIn / FadeOut

class FadeInPushSegue: UIStoryboardSegue {

    var animated: Bool = true

    override func perform() {

        if var sourceViewController = self.sourceViewController as? UIViewController, var destinationViewController = self.destinationViewController as? UIViewController {

            var transition: CATransition = CATransition()

            transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
            sourceViewController.view.window?.layer.addAnimation(transition, forKey: "kCATransition")
            sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)


        }
    }

}

class FadeOutPopSegue: UIStoryboardSegue {

    override func perform() {

        if var sourceViewController = self.sourceViewController as? UIViewController, var destinationViewController = self.destinationViewController as? UIViewController {

            var transition: CATransition = CATransition()

            transition.duration = 0.4
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade

            sourceViewController.view.window?.layer.addAnimation(transition, forKey: "kCATransition")
            sourceViewController.navigationController?.popViewControllerAnimated(false)
        }
    }

}