Why Can't I Call My Function with Gesturetaprecogniser

Adding tap Gesture that calls a function in another class

Since the object is a local variable it's deallocated after functions ends so make it an instance variable inside its class

let selectMoment = SelectMomentClass(countUserMoment: 1, cardView: self.cardView)

GestureRecognizer not responding to tap

I discovered the answer after carefully combing through my code.

One of the parent views was created without supplying a frame:

While it's a noobish enough error to warrant deletion of this questions, odds are someone else will also have the same issue in the future...

Gesture Recognizer Not Called

Set the gesture recognizer delegate to yourself and add the UIGestureRecognizerDelegate protocol.

func keyboardWillShow(notification: NSNotification) {
if keyboardDismissTapGesture == nil
{
println("dismiss")
keyboardDismissTapGesture = UITapGestureRecognizer(target: view, action: "dismissKeyboard:")
keyboardDismissTapGesture.delegate = self
self.view.addGestureRecognizer(keyboardDismissTapGesture!)
}
}

Then add:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
return true
}

My guess is, the gesture of your table view is interfering with the new UITapGesture. Try this solution or else you should fail the gesture of your table view when your new UITapGesture is detected.

To fail the UITapGestureRecognizer of the table view I use this code:

if let recognizers = yourTableView.gestureRecognizers, let index = find(recognizers.map { $0 is UITapGestureRecognizer }, true) {
(recognizers[index] as! UIPanGestureRecognizer).requireGestureRecognizerToFail(keyboardDismissTapGesture)
}

Maybe not the most elegant way of doing it, but it works for me when I want to fail the UIPanGestureRecognizer. I haven't tested it with the UITapGestureRecognizer.

EDIT:

if let recognizers = yourTableView.gestureRecognizers, let index = find(recognizers.map { $0 is UIGestureRecognizer }, true) {
(recognizers[index] as! UIGestureRecognizer).requireGestureRecognizerToFail(keyboardDismissTapGesture!)
}

Tap Gesture Recognizer Interfering with Clear Button

Figured out a way to prevent this from happening.

So the clear button is of type UIButton, so we don't want the gesture recognizer to recognize those taps.

This function is called when the gesture recognizer receives a tap:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
// Don't handle button taps
return !(touch.view is UIButton)
}

The line:

return !(touch.view is UIButton)

Will return false if the place the user tapped was a UIButton and true otherwise.

So false = tap will not be handled by the gesture recognizer, and true = tap will be handled.

Don't forget to add the UIGestureRecognizerDelegate and set it using the following line:

tap.delegate = self

didSelectRowAtIndexPath not being called because of UI tap Gesture Swift Solution?

The solution is to set the property cancelsTouchesInView of your TapAwayFromTextEditTapGesture to false. This will cause it to pass on touches to the table view.

From the Apple documentation on this property (with my emphasis):

When this property is true (the default) and the receiver recognizes its gesture, the touches of that gesture that are pending are not delivered to the view and previously delivered touches are cancelled through a touchesCancelled(_:with:) message sent to the view. If a gesture recognizer doesn’t recognize its gesture or if the value of this property is false, the view receives all touches in the multi-touch sequence.

Why does this UITapGestureRecognizer not recognize taps?

Your set up should work, so I would guess that one of two things is happening. You may have forgotten to actually add this view to the ViewController's view, or there maybe another view placed on top of this view which is stealing the touches?

Gesture recognizer and button actions

In the "shouldReceiveTouch" method you should add a condition that will return NO if the touch is in the button.

This is from apple SimpleGestureRecognizers example.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == yourButton)) {//change it to your condition
return NO;
}
return YES;
}

hope it will help

Edit

As Daniel noted you must conform to UIGestureRecognizerDelegate for it to work.

shani



Related Topics



Leave a reply



Submit