闭包

//
//  ViewController.swift
//  闭包
//
//  Created by 谢泽锋 on 2017/5/10.
//  Copyright © 2017年 泽锋. All rights reserved.
//

import UIKit

class TwoViewController: UIViewController {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss(animated: true, completion: nil);
    }
    deinit{
        print("销毁了");
    }
    
    /// 如果在某个类中定义一个属性,那么这个属性必需初始化,否则就会报错
    
    /// 如果暂时不想初始化,那么必须在后面写上❓问号
    var finishs:(()->())?;//当前写法代表闭包的返回值可以是nil
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        /*
         闭包的写法
         1.闭包通过实参传递给函数
         2.如果必保湿函数的最后一个参数,那么闭包可以写在函数()的后面
         3.如果函数只接受一个参数,并且这个参数是闭包,那么()可以省略
         */
        weak var weakSelf = self;
        loadData {
            print("执行完成");
            weakSelf?.view.backgroundColor = UIColor.green;
            view.backgroundColor = UIColor.orange;
        };
        loadDataTWO(num: 23 ,finish: {
            print("第一种完成");

            //循环引用
//            view.backgroundColor = UIColor.orange;
//            self.view.backgroundColor = UIColor.orange;
            weakSelf?.view.backgroundColor = UIColor.orange;

        });
        //2
        loadData() {
            print("执行完成");
        };
        loadDataTWO(num: 23) {
            
        };
        //3
        loadData {
            
        };
        /*闭包的简写
         如果闭包没有参数也没有返回值,那么in之前的东西可以删除,包括in
         */
        //第一种
        //        let sc = UIScrollView(frame: CGRect(x: 0, y: 200, width: 400, height: 44));
        //        sc.backgroundColor = UIColor.red;
        //        view.addSubview(sc);
        //        let width = 80;
        //        let count = 15;
        //        for i in 0..<count{
        //            let btn = UIButton();
        //            btn.backgroundColor = UIColor.orange;
        //            btn.setTitle("标题\(i)", for: UIControlState.normal);
        //            btn.frame = CGRect(x: i*width+1, y: 0, width: width, height: 44);
        //            sc.addSubview(btn);
        //        }
        //        sc.contentSize = CGSize(width: width*count, height: 44);
        //第二种
        //      let sc = creatScrollView { () -> Int in
        //        return 10;
        //        };
        //        view.addSubview(sc);
        
        
        
        //        第三种
        
        let sc = creatScrollView(btnCount: { () -> Int in
            return 10;
        }) { (index) -> UIView in
            let width = 80;
            
            let btn = UIButton();
            btn.backgroundColor = UIColor.orange;
            btn.setTitle("标题\(index)", for: UIControlState.normal);
            btn.frame = CGRect(x: index*width+1, y: 0, width: width, height: 44);
            return btn;
        }
        view.addSubview(sc);
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    func creatScrollView(btnCount:()->Int) ->UIScrollView{
        let sc = UIScrollView(frame: CGRect(x: 0, y: 200, width: 400, height: 44));
        sc.backgroundColor = UIColor.red;
        let width = 80;
        let count = btnCount();
        for i in 0..<count{
            let btn = UIButton();
            btn.backgroundColor = UIColor.orange;
            btn.setTitle("标题\(i)", for: UIControlState.normal);
            btn.frame = CGRect(x: i*width+1, y: 0, width: width, height: 44);
            sc.addSubview(btn);
        }
        sc.contentSize = CGSize(width: width*count, height: 44);
        
        return sc;
    }
    
    func creatScrollView(btnCount:()->Int,btnwithIndex:(_ index:Int)->UIView) ->UIScrollView{
        let sc = UIScrollView(frame: CGRect(x: 0, y: 200, width: 400, height: 44));
        sc.backgroundColor = UIColor.red;
        let width = 80;
        let count = btnCount();
        for i in 0..<count{
            //            let btn = UIButton();
            //            btn.backgroundColor = UIColor.orange;
            //            btn.setTitle("标题\(i)", for: UIControlState.normal);
            //            btn.frame = CGRect(x: i*width+1, y: 0, width: width, height: 44);
            //            sc.addSubview(btn);
            let subview = btnwithIndex(i);
            sc.addSubview(subview);
            
        }
        sc.contentSize = CGSize(width: width*count, height: 44);
        
        return sc;
    }
    
    
    
    func loadDataTWO(num:Int, finish:@escaping ()->()) {
        print("执行耗时操作")
        self.finishs = finish;
        finish();
    }
    
    
    func loadData(finish:()->()) {
        print("执行耗时操作")
        finish();
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


posted @ 2017-05-10 12:43  谢小锋  阅读(121)  评论(0编辑  收藏  举报