UIViewController切换及简单动画

前端之家收集整理的这篇文章主要介绍了UIViewController切换及简单动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

UIViewController应该是我们最常用的。

那么最简单的切换方式是什么了?

func presentViewController(viewControllerToPresent: UIViewController,animated flag: Bool,completion: (() -> Void)?)


同时可以设置一些简单动画,淡入淡出,翻页等等

我们首先创建一个项目,他会有一个自带的视图控制器ViewController

然后我们再创建视图控制器用来切换。

import UIKit

class MyRootViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor=UIColor.lightGrayColor()
        
        let btn=UIButton(frame: CGRectMake(20,120,320,36))
        btn.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        btn.setTitle("返回",forState: UIControlState.Normal)
        btn.addTarget(self,action: "back",forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func back(){
        self.dismissViewControllerAnimated(true,completion: nil)
    }
    

}

我们可以看到这个类很简单就是一个UIViewController的子类。

给他创建button用来返回。


接下来,我们给自带的ViewController添加四个Button分别测试四种切换方式

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let btn=UIButton(frame: CGRectMake(20,100,36))
        btn.setTitle("垂直切换",forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        btn.tag=90
        btn.addTarget(self,action: "testAct:",forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn)
        
        
        let btn1=UIButton(frame: CGRectMake(20,150,36))
        btn1.setTitle("旋转",forState: UIControlState.Normal)
        btn1.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        btn1.tag=91
        btn1.addTarget(self,forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn1)
        
        let btn2=UIButton(frame: CGRectMake(20,200,36))
        btn2.setTitle("淡入淡出",forState: UIControlState.Normal)
        btn2.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        btn2.tag=92
        btn2.addTarget(self,forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn2)
        
        let btn3=UIButton(frame: CGRectMake(20,250,36))
        btn3.setTitle("翻页",forState: UIControlState.Normal)
        btn3.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        btn3.tag=93
        btn3.addTarget(self,forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn3)
        
        
    }


button点击后对应的方法如下
    func testAct(b:UIButton){
        let rootVC=MyRootViewController()
        
        
        
        if(b.tag==90){
            rootVC.modalTransitionStyle = .CoverVertical
        }else if(b.tag==91){
            rootVC.modalTransitionStyle = .FlipHorizontal
        }else if(b.tag==92){
            rootVC.modalTransitionStyle = .CrossDissolve
        }else if(b.tag==93){
            rootVC.modalTransitionStyle = .PartialCurl
        }
        
        self.presentViewController(rootVC,animated: true,completion: nil)
        
        
    }

其中modalTr5ansitionStyle 是一个枚举,有四个值,分别代表四种奇幻方式

我们测试一下看效果吧。有问题欢迎加群讨论

苹果开发群 :414319235 欢迎加入 欢迎讨论问题

猜你在找的Swift相关文章