简体   繁体   English

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

[英]Present New ViewController programmatically [Swift 3]

What I am trying to do is present a new view controller programmatically with Swift 3 with an animated fade-out then fade-in and a pop-up then pop-down depending on what view controllers the user enters. 我想要做的是以Swift 3编程方式呈现一个新的视图控制器,带有动画淡出然后淡入,然后根据用户输入的视图控制器弹出一个弹出窗口。 This is so my app feels more modern and less old and blocky. 这是我的应用程序感觉更现代,更老和块状。

Here is the current method I am using for presenting a new view controller. 这是我用于呈现新视图控制器的当前方法。 It works, but it is rather abrupt and robotic: 它有效,但它是相当突然和机器人:

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

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

here is a method for animating UIViews but it doesnt work with UIViewControllers 这是一个动画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)
    }

i need something similar to this but for a UIViewController 我需要类似于此的东西但是对于UIViewController

UPDATE UPDATE

here is what you would use to to present a new viewcontroller in a cool way to make your app more modern 这里是你用来以一种很酷的方式呈现一个新的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)

if you have any questions comment them so i can help you guys out 如果你有任何问题评论他们,所以我可以帮助你们

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

Check this code for animation to view controller 检查此代码以查看控制器的动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM