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")
}
}
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With