以编程方式呈现新的ViewController [Swift 3]

时间:2017-09-25 14:57:17

标签: ios swift animation uiviewcontroller

我要做的是以Swift 3编程方式呈现一个新的视图控制器,其中包含动画淡出然后淡入,然后根据用户输入的视图控制器弹出一个弹出窗口。这是我的应用程序感觉更现代,更老和块。

这是我用于呈现新视图控制器的当前方法。它有效,但它是相当突然和机器人:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let ExampleVC = storyBoard.instantiateViewController(withIdentifier: "ExampleVC")
//ExampleVC standing for ExampleViewController 
self.navigationController?.pushViewController(ExampleVC, animated: false) 

这是一个动画UIViews的方法,但它不适用于UIViewControllers

func popIn(yourView : UIView) {

        yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: {
            yourView.transform = .identity
        }, completion: nil)
        self.view.addSubview(yourView)
    }

我需要类似于此的东西,但对于UIViewController

更新

这里是你用来以一种很酷的方式呈现一个新的viewcontroller以使你的应用更现代化的方法

let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let VC = storyboard.instantiateViewController(withIdentifier: "Example") //<<< make sure you enter in your storyboard ID here or you will crash your app
    VC.modalTransitionStyle = .crossDissolve //you can change this to do different animations
    VC.view.layer.speed = 0.1 //adjust this to animate at different speeds
    self.navigationController?.present(VC, animated: true, completion: nil)

如果你有任何问题评论他们,那么我可以帮助你们

1 个答案:

答案 0 :(得分:3)

var storyboard = UIStoryboard(name: "Main", bundle: nil)
var ivc = storyboard.instantiateViewController(withIdentifier: "ExampleVC")
ivc.modalTransitionStyle = .crossDissolve
ivc.view.layer.speed = 0.1
self.present(ivc, animated: true, completion: { _ in })

检查此代码以查看动画以查看控制器