美文网首页
IOS:Swift-模态

IOS:Swift-模态

作者: 任任任任师艳 | 来源:发表于2016-12-02 15:28 被阅读0次

    AppDelegate.swift:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController = FirstViewController()
       
        return true
    } 
    
    FirstViewController.swift:
    
    class FirstViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
            // Do any additional setup after loading the view.
            let presentbutton = UIButton(type: .custom)
            presentbutton.frame = CGRect(x: 100, y: 150, width: 214, height: 40)
            presentbutton.setTitle("模态视图", for: .normal)
            presentbutton.backgroundColor = UIColor.cyan
                presentbutton.setTitleColor(UIColor.gray, for: .highlighted)
            presentbutton.addTarget(self, action: #selector(presentAction), for: .touchUpInside)
            self.view.addSubview(presentbutton)
        }
    //presentAction的关联方法
        func presentAction(sender:UIButton)  {
            //先要创建模态视图控制器对象
            let secondVC = SecondViewController()
            //模态出带有导航条的视图,就把这个视图加到一个导航视图控制器,但是此时要模态导航视图控制器
            let rootVC = UINavigationController(rootViewController: secondVC)
           
            //设置要出视图的动画效果
            //翻页效果
            secondVC.modalTransitionStyle = .partialCurl
           
    
           
            //模态视图  参数1:要模态出的视图对象 参数2;要不要动态效果 参数3:模态时要做的操作写在闭包里 nil
            self.present(rootVC, animated: true, completion: nil)
           
           
           
        } 
        //点击方法
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            let alertVC = UIAlertController(title: "警告", message: "严禁游泳", preferredStyle: .alert)
    //        let alertVC = UIAlertController(title: "警告", message: "严禁游泳", preferredStyle: .actionSheet)
            self.present(alertVC, animated: true, completion: nil)
            //按钮
            let action = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            //
            let photoLibrayAcyion = UIAlertAction(title: "相册", style: .destructive, handler: {(sender:UIAlertAction) in
                print("选择了相册")
                })
            let cameraAction = UIAlertAction(title: "相机 ", style: .destructive, handler: {(sender:UIAlertAction) in
                print("选择了后摄像头")
                })
           
            //添加按钮
           
            alertVC.addAction(photoLibrayAcyion)
            alertVC.addAction(cameraAction)
            //取消样式的按钮在下面显示
            alertVC.addAction(action)
        }
    
    SecondViewController.swift 
    class SecondViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
            // Do any additional setup after loading the view.
            //完成模态实行的效果
            let dismissbutton = UIButton(type: .custom)
            dismissbutton.frame = CGRect(x: 100, y: 150, width: 214, height: 40)
            dismissbutton.setTitleColor(UIColor.yellow, for: .normal)
            dismissbutton.setTitle("模态消失视图", for: .normal)
            dismissbutton.setTitleColor(UIColor.blue, for: .highlighted)
            dismissbutton.backgroundColor = UIColor.brown
            self.view.addSubview(dismissbutton)
            dismissbutton.addTarget(self, action: #selector(dismissAction), for: .touchUpInside)
           
        }
    //MARK:-dismissAction的关联方法
        func dismissAction(sender:UIButton) {
            //让模态出来的视图消失
            self.dismiss(animated: true, completion: nil)
           
        }
    
    

    相关文章

      网友评论

          本文标题:IOS:Swift-模态

          本文链接:https://www.haomeiwen.com/subject/xthqmttx.html