zoukankan      html  css  js  c++  java
  • swift 第十三课 GCD 的介绍和使用

    手头的项目中基本没有用到这个,但是还是要探索下。毕竟好多的地方要用这个,而且现在手机和电脑等电子设备都是多核心的,这样就成就了多线程带来更加优越的用户体验。

    先记录下,自己看到的两个不错的连接:

    http://www.cocoachina.com/swift/20161116/18097.html

    http://www.jianshu.com/p/f042432e2d7d

    上面这两篇博客写的相当的详细,比我自己写的好多了 (自己很惭愧 )

    这里主要复制贴上自己的使用的最简单的代码好了……(笑笑的偷懒下……)


    import UIKit

    class ViewController: UIViewController {

        let debug = 0 // 修改这个值  0 测试gcd ; 1 测试timer
        
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

            if debug == 0 {
                self.testGCD()
            }else{
                self.setTimer()
                self.perform(#selector(stopTimer), with: nil, afterDelay: 30)
            }
        }
        /**
         gcd
         */
        func testGCD()  {
            
            print("DispatchQueue.main.sync: befor  这个应该是主线程吧", Thread.current)
            DispatchQueue.global().async {
                print("DispatchQueue.global().async: Time task 这个是并发队列", Thread.current, " --: 耗时操作在后台线程中执行!")
                
                DispatchQueue.main.async {
                    print("DispatchQueue.main.async: update UI", Thread.current, " --: 耗时操作执行完毕后在主线程更新 UI 界面!")
                }
            }
            print("DispatchQueue.main.sync: after 主线程", Thread.current)
        }
        
        /**
         gcd timer
         */
        
        private var timer: DispatchSourceTimer?
        var pageStepTime: DispatchTimeInterval = .seconds(5)
        
        func setTimer()  {
            
            print("timer 的启动代码")
            /*
              deadline 结束时间
              interval 时间间隔
              leeway  时间精度
             */
            timer = DispatchSource.makeTimerSource(queue: .main)
            timer?.scheduleRepeating(deadline: .now() + pageStepTime, interval: pageStepTime)
            timer?.setEventHandler {
                
                self.changeViewColor()
            }
            // 启动定时器
            timer?.resume()
        }
        
        func stopTimer(){
           
            print("timer 的终止代码")
            if let time = self.timer {
                time.cancel()
                timer = nil
            }
        }
        
        func changeViewColor()  {
            
            /*
             创建个随机色
             */
            let red = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
            let green = CGFloat( arc4random_uniform(255))/CGFloat(255.0)
            let blue = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
            let colorRun = UIColor.init(red:red, green:green, blue:blue , alpha: 1)
          
            /*
             切换个颜色
             */
            self.view.backgroundColor = colorRun
        }

        override func viewDidLoad() {
            super.viewDidLoad()
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    }
  • 相关阅读:
    html5新增元素和废除元素
    html5本地存储
    第四章表单与文件笔记新增属性
    第五章canvas
    lable中for的作用
    第四章表单与文件学习内容
    第三章html5的结构
    html5的全局属性
    正则表达式—升华
    微前端
  • 原文地址:https://www.cnblogs.com/Bob-blogs/p/6593046.html
Copyright © 2011-2022 走看看