【问题标题】:Present New ViewController programmatically [Swift 3]以编程方式呈现新的 ViewController [Swift 3]
【发布时间】:2017-09-25 14:57:16
【问题描述】:

我想要做的是使用 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

更新

这是您用来以一种很酷的方式呈现新的视图控制器以使您的应用更现代的方法

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)

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

【问题讨论】:

    标签: ios swift animation uiviewcontroller


    【解决方案1】:
    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 })
    

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

    【讨论】:

    • 虽然看起来很奇怪,但我一点也不喜欢它(P.S. 检查我上面的代码中的一个函数,它显示 CollectionView Cells 真的很酷,就像
    • 好的,我上面的代码从屏幕中心以弹出窗口(它从非常小的尺寸放大到设定的尺寸)样式呈现 UIView 我经常使用它来为其他人呈现数据事情,但它不适用于 UIViewControllers 仅 UIView
    • 哥们,听起来很酷,我试试看,但有什么办法可以减慢速度?
    • ivc.view.layer.speed = 0.1 试试这个来改变速度。希望它有效
    • 如果您对我的回答感到满意,也请投票!!谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    相关资源
    最近更新 更多