在swift中为按钮添加手势

时间:2015-01-21 03:57:57

标签: ios xcode swift

我尝试在我的视图中为现有按钮添加手势(滑动)功能,但我无法弄清楚如何将滑动附加到按钮区域。

理想的效果是有一个我可以按下的按钮以及滑动以产生不同的结果。到目前为止,我实现手势的方式将其应用于我的整个视图而不仅仅是按钮。

我觉得这很简单,但它已经逃脱了我几天 - 我可能只是在寻找错误的东西。

(我正在分配' @IBOutlet var swipeButton:UIButton!'到我的按钮BTW)

以下代码:

class ViewController: UIInputViewController {

@IBOutlet var swipeButton: UIButton!

let swipeRec = UISwipeGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()
    self.loadInterface()

    var swipeButtonDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "ButtonDown")
    swipeButtonDown.direction = UISwipeGestureRecognizerDirection.Down
    self.view.addGestureRecognizer(swipeButtonDown)
}

@IBAction func buttonPressed(sender: UIButton) {
    var proxy = textDocumentProxy as UITextDocumentProxy
    proxy.insertText("button")
}
func buttonDown(){
    var proxy = textDocumentProxy as UITextDocumentProxy
    proxy.insertText("swipe")
}

}

1 个答案:

答案 0 :(得分:6)

如果你想将swipeGesture添加到Button中,那就这样做:

self.yourButton.addGestureRecognizer(swipeButtonDown)

并且您发送的选择器中也存在错误:

var swipeButtonDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "buttonDown")

ButtonDown更改为buttonDown