2016-07-19 87 views

回答

0

首先,你必須創建IBAction爲例如:changeTextField(看你的截圖)

screenshot: create an IBAction for Editing changed event

其次添加以下代碼

@IBAction func changeTextField(sender: AnyObject) { 
    NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(ViewController.functionWithDelay), object: nil) 
    self.performSelector(#selector(ViewController.functionWithDelay), 
         withObject: nil, 
         afterDelay: 1/4); 
} 

第三,你的功能

func functionWithDelay() { 
    print("running...") 
} 
+0

這不是我想要的。 每次按下鍵盤時都會調用「functionWithDelay」。 我希望僅在不超過1/4秒時纔會調用該函數 –

+0

代碼更新:如果在1/4秒之前敲擊鍵盤,我添加了對cancelPreviousPerformRequestsWithTarget的調用 – David

+0

這很完美,謝謝:D –