使用闭包的小案例(一)传参和返回值

需求描述:

在控制器的view上创建一个scrollView,里面添加若干个button

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let sc = UIScrollView()
        sc.backgroundColor = UIColor.red
        let buttonW = 80
        let btnCount = 8
        for i in 0..<btnCount
        {
            let btn = UIButton()
            btn.backgroundColor = UIColor.green
            btn .setTitle("按钮\(i)", for: UIControlState.normal)
            btn.frame = CGRect(x:buttonW*Int(i), y:0, width:buttonW, height:44)
            sc.addSubview(btn)
        }
        sc.frame = CGRect(x:0, y:100, width:view.bounds.size.width, height: 44)
        sc.contentSize = CGSize(width:8*buttonW, height:44)
        view.addSubview(sc)
    }
}

需求描述:用函数实现上面需求,并且创建button个数使用闭包返回

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let sc = creatSubViewsWithCount(buttonCount: { () -> (Int) in
            return 8
        })
        view.addSubview(sc)
    }

    func creatSubViewsWithCount(buttonCount:() -> (Int)) -> UIScrollView {
        let sc = UIScrollView()
        sc.backgroundColor = UIColor.red
        let buttonW = 80
        let btnCount = buttonCount()
        for i in 0..<btnCount
        {
            let btn = UIButton()
            btn.backgroundColor = UIColor.green
            btn .setTitle("按钮\(i)", for: UIControlState.normal)
            btn.frame = CGRect(x:buttonW*Int(i), y:0, width:buttonW, height:44)
            sc.addSubview(btn)
        }
        sc.frame = CGRect(x:0, y:100, width:view.bounds.size.width, height: 44)
        sc.contentSize = CGSize(width:8*buttonW, height:44)
        return sc
    }
}

需求描述:用函数实现上面需求,并且创建子控件个数使用闭包返回,子控件类型是什么,也由另一个闭包决定(即灵活性好,易扩展)

分析:
闭包1返回参数: 返回创建子控件的个数
闭包2传入参数:第几个子控件的索引
闭包2返回参数:第几个新创建的子控件

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let sc = creatSubViewsWithCount(buttonCount: { () -> (Int) in
            return 8
        }, buttonWithCount: { (index) -> (UIButton) in
            let buttonW = 80
            let btn = UIButton()
            btn.backgroundColor = UIColor.green
            btn .setTitle("按钮\(index)", for: UIControlState.normal)
            btn.frame = CGRect(x:buttonW*Int(index), y:0, width:buttonW, height:44)
            return btn
        })
        view.addSubview(sc)
    }

    func creatSubViewsWithCount(buttonCount:() -> (Int),buttonWithCount:(Int) -> (UIButton)) -> UIScrollView {
        let sc = UIScrollView()
        sc.backgroundColor = UIColor.red
        let buttonW = 80
        let btnCount = buttonCount()
        for i in 0..<btnCount
        {
            let btn = buttonWithCount(i)
            sc.addSubview(btn)
        }
        sc.frame = CGRect(x:0, y:100, width:view.bounds.size.width, height: 44)
        sc.contentSize = CGSize(width:8*buttonW, height:44)
        return sc
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,678评论 25 708
  • 今天的博客算是比较基础的,还是那句话,基础这东西在什么时候都是最重要的。说到函数,只要是写过程序就肯定知道函数是怎...
    攞你命3OOO阅读 603评论 0 1
  • 函数和对象 1、函数 1.1 函数概述 函数对于任何一门语言来说都是核心的概念。通过函数可以封装任意多条语句,而且...
    道无虚阅读 4,665评论 0 5
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,219评论 30 472
  • 根本的幸福来源于什么?每个人的感受不同,答案也不同。也根本在于你是否抓住了问题的关键,知道自己到底为什么不幸福。而...
    birch阅读 168评论 0 1