在 swift 的自定义键盘中为所有 "buttons" 添加单击、双击和三次点击手势
Add single, double and triple tap gestures to all "buttons" in custom keyboard in swift
我正在尝试创建一个允许单击、双击和三次单击的键盘。所以我想为键盘上的每个按钮添加一个 UITapGestureRecognizer() 。我知道如何从 xib 文件手动执行此操作(添加每个字母它自己的手势,这需要很长时间)但不太确定如何在控制器中执行此操作。
我在 viewDidLoad() 方法中为双击而写的:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTap.numberOfTapsRequired = 2
for button in self.view.subviews{
button.addGestureRecognizer(doubleTap)
}
并创建了一个 doubleTapCharacter() 方法,但它仍然无法正常工作。我还希望能够将信息发送到 doubleTapCharacter 方法。
如有任何帮助,我们将不胜感激。另外,我是 swift 的新手,所以如果说明很复杂,如果您能稍微分解一下,我将不胜感激。
创建并添加手势识别器:
for button in view.subviews {
// create the gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTapRecognizer.numberOfTapsRequired = 2
// add gesture recognizer to button
button.addGestureRecognizer(doubleTapRecognizer)
}
然后实现目标方法:
func doubleTapCharacter(doubleTapRecognizer: UITapGestureRecognizer) {
let tappedButton = doubleTapRecognizer.view as! UIButton
print(tappedButton.titleForState(UIControlState.Normal))
}
我正在尝试创建一个允许单击、双击和三次单击的键盘。所以我想为键盘上的每个按钮添加一个 UITapGestureRecognizer() 。我知道如何从 xib 文件手动执行此操作(添加每个字母它自己的手势,这需要很长时间)但不太确定如何在控制器中执行此操作。
我在 viewDidLoad() 方法中为双击而写的:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTap.numberOfTapsRequired = 2
for button in self.view.subviews{
button.addGestureRecognizer(doubleTap)
}
并创建了一个 doubleTapCharacter() 方法,但它仍然无法正常工作。我还希望能够将信息发送到 doubleTapCharacter 方法。
如有任何帮助,我们将不胜感激。另外,我是 swift 的新手,所以如果说明很复杂,如果您能稍微分解一下,我将不胜感激。
创建并添加手势识别器:
for button in view.subviews {
// create the gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTapRecognizer.numberOfTapsRequired = 2
// add gesture recognizer to button
button.addGestureRecognizer(doubleTapRecognizer)
}
然后实现目标方法:
func doubleTapCharacter(doubleTapRecognizer: UITapGestureRecognizer) {
let tappedButton = doubleTapRecognizer.view as! UIButton
print(tappedButton.titleForState(UIControlState.Normal))
}