swift中UIViewController的使用

来源:互联网 发布:私人订制软件 编辑:程序博客网 时间:2024/05/17 06:02

源码:https://github.com/potato512/SYSwiftLearning


UIViewController视图控制器在iOS研发中不可或缺,基本上每一个页都的研发都会使用到。

在使用过程中,主要使用了以下几个方面。

1、视图控制器的属性设置。如背景颜色,适配,视图控制器数组属性等

2、视图控制器的生命周期的控制

3、视图控制器间的转场present,或push,以及相对应的dismiss,或pop

……

// 生命周期// 视图已经加载完成override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view.                       print("2 viewDidLoad")}// 已经收到内存警告override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.                print("? didReceiveMemoryWarning")}// 加载视图    override func loadView() {                super.loadView()                print("1 loadView")}    // 视图即将出现override func viewWillAppear(animated: Bool) {        super.viewWillAppear(animated)                print("3 viewWillAppear")}// 视图即将布局    override func viewWillLayoutSubviews() {        super.viewWillLayoutSubviews()                print("4 viewWillLayoutSubviews")}// 视图已经布局    override func viewDidLayoutSubviews() {        super.viewDidLayoutSubviews()                print("5 viewDidLayoutSubviews")}    // 视图已经出现override func viewDidAppear(animated: Bool) {        super.viewDidAppear(animated)                print("6 viewDidAppear")}    // 视图即将消失override func viewWillDisappear(animated: Bool) {        super.viewWillDisappear(animated)                print("7 viewWillDisappear")}    // 视图已经消失override func viewDidDisappear(animated: Bool) {        super.viewDidDisappear(animated)                print("8 viewDidDisappear")}    // 释放视图控制器deinit{        print("9 \(self) 被释放")}


// MARK: - 适配func autoSize(){        if self.respondsToSelector(Selector("edgesForExtendedLayout"))        {            self.edgesForExtendedLayout = UIRectEdge.None        }                if self.respondsToSelector(Selector("extendedLayoutIncludesOpaqueBars"))        {            self.extendedLayoutIncludesOpaqueBars = false        }                if self.respondsToSelector(Selector("automaticallyAdjustsScrollViewInsets"))        {            self.automaticallyAdjustsScrollViewInsets = false        }}
// MARK: - 根视图控制器var isRootViewController:Bool{        get        {            if self.navigationController!.viewControllers.first!.isEqual(self)            {                return true            }                        return false        }}
 // MARK: - 视图控制器索引下标值var indexViewController:Int{        get        {            let indexVC = self.navigationController!.viewControllers.indexOf(self)!            return indexVC        }}
// MARK: - 返回上层视图控制器func backPreviousController(){        if self.isRootViewController        {            self.dismissViewControllerAnimated(true, completion: nil)        }        else        {            if (self.presentedViewController != nil)            {                self.dismissViewControllerAnimated(true, completion: nil)            }            else            {                self.navigationController!.popViewControllerAnimated(true)            }        }}
override func loadView() {                super.loadView()                // 视图控制器背景颜色        self.view.backgroundColor = UIColor.whiteColor()}
// present视图控制器let nextVC = PresentViewController()let nextNav = UINavigationController(rootViewController: nextVC)/*视图控制器翻转效果由下向上推出(默认模式) CoverVertical水平翻转 FlipHorizontal淡入淡出 CrossDissolve翻页效果 PartialCurl        注意:如果有导航视图控制器时,翻转效果设置在导航视图控制器;没有时则设置在视图控制器。*/nextNav.modalTransitionStyle = UIModalTransitionStyle.PartialCurl        self.presentViewController(nextNav, animated: true, completion: nil)
// 返回上一个视图控制器self.dismissViewControllerAnimated(true, completion: nil)
// push视图控制器let nextVC = PopViewController()// self.navigationController!.pushViewController(nextVC, animated: true)        // 转场动画1UIView.beginAnimations(nil, context: nil)UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut)UIView.setAnimationDuration(0.6)self.navigationController!.pushViewController(nextVC, animated: true)UIView.setAnimationTransition(UIViewAnimationTransition.CurlUp, forView: self.navigationController!.view, cache: false)UIView.commitAnimations()
// 转场动画2let animation = CATransition()animation.duration = 0.6animation.type = kCATransitionRevealanimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)animation.subtype = kCATransitionFromBottomself.navigationController!.pushViewController(nextVC, animated: true)self.navigationController!.view.layer.addAnimation(animation, forKey: nil)
// 返回上一个视图控制器self.navigationController!.popViewControllerAnimated(true)// 返回根视图控制器self.navigationController!.popToRootViewControllerAnimated(true)// 返回指定视图控制器let indexVC = self.navigationController!.viewControllers[2]self.navigationController!.popToViewController(indexVC, animated: true)












0 0