Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding gestures to a button in swift

Tags:

xcode

ios

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

}
like image 237
glasstongue Avatar asked Jan 21 '15 03:01

glasstongue


People also ask

How do I add gestures in Swift?

Adding a Tap Gesture Recognizer to an Image View in Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the image view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

How do you add swipe gestures in swift 5?

We can use the createSwipeGestureRecognizer(for:) method in the view controller's viewDidLoad() method to create a swipe gesture recognizer for each direction. We pass the result of createSwipeGestureRecognizer(for:) to the addGestureRecognizer(_:) method.

What is UITapGestureRecognizer Swift?

UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.

What is Pan gesture in Swift?

A pan gesture occurs any time a person moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.


1 Answers

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

like image 81
Dharmesh Kheni Avatar answered Oct 05 '22 17:10

Dharmesh Kheni