美文网首页
Swift: Closure闭包学习

Swift: Closure闭包学习

作者: 婉卿容若 | 来源:发表于2016-04-22 13:47 被阅读76次

情景: 在一个scrollView上添加n个Button

  • demo01

单纯的无封装无closure


  
        import UIKit

        class ViewController: UIViewController {

        override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //1.创建UIScrollview
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 44))
        scrollView.backgroundColor = UIColor.blueColor()
        
        let width = 80
        //2.创建n多UIButton
        for i  in 0..<15 {
            let btn = UIButton()
            
             //3.设置button属性
            btn.setTitle("标题\(i)", forState: UIControlState.Normal)
            btn.backgroundColor = UIColor.redColor()
            btn.frame = CGRect(x: i*width, y: 0, width: width, height: 44)
           //4.将button添加到UIScrollview上
            scrollView.addSubview(btn)
            
            scrollView.contentSize = CGSize(width: (i+1)*width, height: 44)
        }
        //5.将scrollview添加到view上
        //特点:没有self(self.view) swift推荐能不写self就不写self
        view.addSubview(scrollView)
             }
        }
  • demo02

通过函数进行UIScrollView的创建 - 通过闭包传递按钮的个数、按钮的创建
对入参进行闭包封装,让操作在闭包中完成


import UIKit

class ViewController: UIViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        
        let sc =  setupScrollView({ () -> Int in
            return 10
        }) { (index) -> UIView in
            
            let width = 80
            
            let btn = UIButton()
            //3.设置button属性
            btn.setTitle("标题\(index)", forState: UIControlState.Normal)
            btn.backgroundColor = UIColor.redColor()
            btn.frame = CGRect(x: index*width, y: 0, width: width, height: 44)
            
            return btn
        }
        view.addSubview(sc)
    }
    
    //要求: 定义一个方法来创建UIScrollView,并且UIScrollView上有多少个按钮必须通过闭包告诉该方法
    //并且如何创建按钮需要通过闭包来创建
    func setupScrollView(btnCount:() -> Int,btnWithIndex:(index:Int) -> UIView) -> UIScrollView {
        //1.创建UIScrollview
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 44))
        
        scrollView.backgroundColor = UIColor.blueColor()
        
        let count = btnCount()
        //2.创建n多UIButton
        for i  in 0..<count {
            /*
             let btn = UIButton()
             
             //3.设置button属性
             btn.setTitle("标题\(i)", forState: UIControlState.Normal)
             btn.backgroundColor = UIColor.redColor()
             btn.frame = CGRect(x: i*width, y: 0, width: width, height: 44)
             scrollView.addSubview(btn)
             */
            
            let subView = btnWithIndex(index: i)
            scrollView.addSubview(subView)
            
            scrollView.contentSize = CGSize(width: CGFloat(count)*subView.bounds.width, height: 44)
        }
        
        
        
        //4.将button添加到UIScrollview上
        
        //5.将scrollview添加到view上
        //特点:没有self(self.view) swift推荐能不写self就不写self
        return scrollView
        
    }
}

相关文章

  • Swift: Closure闭包学习

    情景: 在一个scrollView上添加n个Button demo01 单纯的无封装无closure demo02...

  • Swift5.0 - day4-闭包、属性、方法、下标

    一、闭包 1.1、闭包表达式(Closure Expression)在 Swift 里面可以通过函数 func 定...

  • 闭包

    学习Javascript闭包(Closure)

  • Swift 5.3 - SE-0279 Multiple Tra

    在最初 Swift 的定义中,当方法的最后一个参数为闭包时,称该闭包为尾随闭包(trailing closure)...

  • Swift closure闭包

    究竟什么是Closure? 说的通俗一点,一个函数加上它捕获的变量一起,才算一个closure //MARK: -...

  • swift:Closure 闭包

    @noescape @autoclosure @autoclosure(escaping)

  • Swift Closure闭包

    函数也是一种闭包 console log 如下 闭包表达式 console log 如下 闭包中的参数类型推断 c...

  • swift 闭包(closure)

    闭包:就是一个函数和它所捕获的变量/常量环境组合起来,称为闭包。1.一般定义在函数内部的函数。2.一般它捕获的是外...

  • Swift 闭包

    @Author Swift 闭包(Closure) 闭包是一种可以在代码中作为参数传递,自含的功能块。 闭包类似于...

  • swift 学习(4)闭包 (Closure)

    1,闭包 (Closure) Closures are so named because they have th...

网友评论

      本文标题:Swift: Closure闭包学习

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