Swift - UITextField - 手势识别器延迟

时间:2016-07-19 08:46:16

标签: swift uigesturerecognizer delay textfield

我正在使用UITextField并且我想在用户在文本字段中录制时触发一个功能,但我想在触发该功能之前等待1/4秒。

我该怎么做?

1 个答案:

答案 0 :(得分: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...")
}