glasstongue
glasstongue

Reputation: 103

Adding gestures to a button in swift

I'm trying to add gesture(swipe) functionality to an existing button in my view but I can't figure out how to attach the swipe to the button's area.

The desired effect is to have a button I can press as well as swipe to produce different outcomes. So far the way I'm implementing gestures applies it to my entire view not just the button.

I have a feeling it's pretty simple but it's been escaping me for a couple of days - I might just be searching for the wrong thing.

(I'm assigning '@IBOutlet var swipeButton: UIButton!' to my button BTW)

Code below:

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")
}

}

Upvotes: 8

Views: 10868

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

If you want to add swipeGesture into Button then do it like this way:

self.yourButton.addGestureRecognizer(swipeButtonDown)

and also there is a mistake in selector you sent it should be like:

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

change ButtonDown to buttonDown

Upvotes: 9

Related Questions