一個小測試-Runloop

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 不執(zhí)行
       */
        
        /* 2
        while true {
         
           // Already Run 不執(zhí)行
           // 觸摸也不執(zhí)行
           //  線程啟動 沒有添加Runloop需要的數(shù)據(jù)源 && Runloop 沒啟動
        }
        */
       
        /* 3
          RunLoop.current.run()
         //  Already Run 執(zhí)行
         //觸摸也不執(zhí)行
         // Runloop 啟動后由于沒有數(shù)據(jù)源 就立即退出了
         */
        
        
        /*  4
        while true {
             RunLoop.current.run()
         // 觸摸事件有打印 Already Run 不執(zhí)行
        }
       */
        print("Runloop already run")//
    }
    


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容