设为首页 加入收藏

TOP

Swift学习--闭包的简单使用(三)
2017-10-10 12:16:08 】 浏览:6313
Tags:Swift 学习 包的 简单 使用

一.Swift中闭包的简单使用

 override func viewDidLoad() {
        super.viewDidLoad()
      
  /**
        闭包和OC中的Block非常相似
        OC中的block类似于匿名函数
        闭包是用来定义函数
        作用:Block是用于保存一段点,在需要的时候执行
        闭包也是用于保存一段点,在需要的时候执行做一个耗时操作
        */
        /**
        闭包的基本格式:
        {
        (形参列表)->()
        in 
        需要执行的代码
        }
        */
        /**
        *  闭包的几种格式:
          1.将闭包通过实参传递给函数
          2.如果闭包是函数的最后一个参数,那么闭包可以写在函数()的后面
          3.如果函数只接收一个参数,并且这个参数是闭包,那么()可以省略
        */
        loadData (10, finished: {() -> () in
            print("被回调了")
        })
        loadData (10){ () -> () in
            print("被回调了")
        }
        say { () -> () in
            print("hello")
        }
        /**
        闭包的简写:
        如果闭包没有参数也没有返回值,那么,in之前的东西都可以删除,包括in
        */
        loadData(10) {
            print("hahfhahf ")
        }
}



   func say(finish: ()->())
    {
        
    }
    
    func loadData(num: Int, finished: ()->())
    {
        print("执行耗时操作")
        //回调通知调用者
        finished()
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        //Swift中dispatch_async回调的是一个闭包
        dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
            print(NSThread.currentThread())
            print("执行耗时操作")
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                print(NSThread.currentThread())
                print("回到主线程更新UI")
            })
        }
    }

二.使用闭包 创建一个UIScrollView

 override func viewDidLoad() {
        super.viewDidLoad() 
 let sc = createScrollView({ () -> Int in
            return 5
            }) { (index) -> UIView in
                let width = 80
                let btn = UIButton()
                //3.设置按钮的属性
                btn.backgroundColor = UIColor.greenColor()
                btn.setTitle("标题\(index)", forState: UIControlState.Normal)
                btn.frame = CGRect(x: index * width, y: 0, width: width, height: 44)
                //4.返回建好的控件
                return btn
                
        }
        view.addSubview(sc)
}
  //定义一个方法来创建UIScrollview,
    //1.并且UIScrollview上有多少个按钮必须通过闭包告诉该方法
    //2.按钮也通过闭包来创建
    func createScrollView(btnCount: ()-> Int, btnWithIndex: (index:Int) ->UIView) -> UIScrollView
    {
        // 1.创建UIScrollview
        let sc = UIScrollView(frame: CGRect(x: 0, y: 100, width: 375, height: 44))
        sc.backgroundColor = UIColor.redColor()
        
//        let width = 80
        let count = btnCount()
        //添加多个按钮
        for i in 0..<count
        {
//            let btn = UIButton()
//            //设置按钮的属性
//            btn.backgroundColor = UIColor.greenColor()
//            btn.setTitle("标题\(i)", forState: UIControlState.Normal)
//            btn.frame = CGRect(x: i * width, y: 0, width: width, height: 44)
//            //将按钮添加
//            sc.addSubview(btn)
            let subView = btnWithIndex(index: i)
            sc.addSubview(subView)
            sc.contentSize = CGSize(width: CGFloat(count) * subView.bounds.width, height: 44)
        }
        
        
        return sc
    }

 

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Swift学习--常量.变量.数据类型的.. 下一篇Swift学习--闭包中的懒加载(四)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目