iOS 优化iOS开发

一个小测试-Runloop

2017-03-28  本文已影响4人  楼上那位
class JFThread : Thread {
    // override func main() {
      //  print("JFThread main...")
    //}
   
}
// VC 的一个Property
var  myThread: JFThread?

override func viewDidLoad() {   
        super.viewDidLoad()
     
        myThread = JFThread(target: self, selector: #selector(testThread), object: nil)
        myThread!.start()

}

 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //当点击屏幕的时候,会来到performSelector这个方法,而我们可以看到performSelector相当于是sourse
        self.perform(#selector(touchMe), on: myThread!, with: nil, waitUntilDone: false)
    }
    func touchMe () {
        print("toucheme \(Thread.current)")
    }
    func testThread() {
        print("--testThread \(Thread.current)")
        
       /* 1
          RunLoop.current.add(Port(), forMode: .commonModes)
          RunLoop.current.run() // Already Run 不执行
       */
        
        /* 2
        while true {
         
           // Already Run 不执行
           // 触摸也不执行
           //  线程启动 没有添加Runloop需要的数据源 && Runloop 没启动
        }
        */
       
        /* 3
          RunLoop.current.run()
         //  Already Run 执行
         //触摸也不执行
         // Runloop 启动后由于没有数据源 就立即退出了
         */
        
        
        /*  4
        while true {
             RunLoop.current.run()
         // 触摸事件有打印 Already Run 不执行
        }
       */
        print("Runloop already run")//
    }
    


上一篇下一篇

猜你喜欢

热点阅读