GestureRecognizer doesn't work on TableView - ios

I'm trying to find a way to hide the keyboard as soon as the user taps elsewhere on the screen after filtering the content of my tableview.
But somehow when I add an UITapGestureRecognizer my TableView freezes and it's impossible to go further into the application.
Here's what I do :
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tap)
func dismissKeyboard() {
view.endEditing(true)
}
I've tried this on a Blank ViewController with a TextField and it works. What am I missing? Is there a specific way to add GestureRecognizer to table views? Because as soon I add the Gesture it breaks everything. I also tried using resignFirstResponder with the same results.
Any advice? Thanks!

Try setting the cancelsTouchesInView variable to false. This defaults to true, by setting it to false you allow the touches to be responded to by your gesture and then pass through your gesture back down to the view so the tableView can respond as well.
tap.cancelsTouchesInView = false
Documentation on Apple
Discussion Excerpt from Docs:
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:withEvent: 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.

Related

How do I only enable UIPanGestureRecognizer only after UILongPressGesture?

I'm new to swift and wasn't sure if this was possible but I'm running into an issue where I am in a UICollectionView, which is scroll enabled. By default, I only want the scroll enabled. However, if the user holds long enough I want the scroll to be disabled and the UIPanGestureRecognizer enabled. I'm having trouble enabling UIPanGesture after UILongPress. After the pangesture is done, the scroll should be enabled again and pan disabled.
First, the compiler is complaining about your variable has no default value.
You can workaround this by adding an initialize method and set gesture there.
Or you can set the variable as an Optional variable which has default value nil. But after that, you have to unwrap to get real value.
Or if you real know what this is:
class YourViewController: xxxx, yyyy {
lazy var panGesture: UIPanGestureRecognizer! = {
let pan = UIPanGestureRecognizer(…
pan.delegate = self
return pan
}
…
}
Second, you could use UIGestureRecognizerDelegate to help.
At first, your pan gesture is disabled. After your long press, you disable the collection view isScrollEnabled and enable your pan gesture. (And after pan gesture finished, you disable pan gesture and reenable collection view isScrollEnabled)

UITapGesture removing ability to select from UITableView when resigning keyboard responder

So I'm having an issue where I have added a UITapGestureRecognizer to resign first responder status of associated textfields so the keyboard will drop away.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddMedViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
func dismissKeyboard() {
medNameText.resignFirstResponder()
doseAmountText.resignFirstResponder()
}
This works fine for getting rid of the keyboard but not when I want to make a selection from a UITableView that is embedded in the same view. (The text fields are located outside this table view).
Essentially each table view cell represents a pre created specific time that is chosen to match the text entered into the text fields then passed along all together to another view.
Any Idea how I can get the UITableView to still recognize the selection of cell when tapped while resigning first responder of the text fields?
I have tried adding a gestureRecognizer function but as I am new to Swift(and an amateur coder) I am not exaclty sure how it works. Any help would be greatly appreciated!
Thanks.
try to add tap.cancelsTouchesInView = false
It means touches are delivered to a view when a gesture is recognized.

Gesture Recogniser for all events

I have this piece of code
let window = UIApplication.sharedApplication().keyWindow
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
window?.addGestureRecognizer(tap)
It only recognizes Tap (as its tap gesture recognizer), however I want some recognizer to work for all, scroll, pinch, etc.
Also at later point of time I am removing this recognizer.
I read touches began, but how to use it on UIApplication.sharedApplication().keyWindow?
And how to remove that touches began later?
If you want to monitor all touch event then you can subclass UITapGestureRecognizer, this link should help: Monitoring all iOS touches

selector is not called 2 tapgesture

I have added a UITapGestureRecognizer to a view, but when I click it the method is not being called.
func addTapGestuere(uiview: UIView) {
let tapGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("cardTapped:"))
uiview.addGestureRecognizer(tapGestureRecognizer)
}
I run this on the viewDidload
self.addTapGestuere(self.Card1View)
self.addTapGestuere(self.Card2View)
I put a break point on the method
cardTapped(recognizer: UITapGestureRecognizer) {
}
but when I click on the image the method isnt called. I have user interaction enabled for all the views.
Be careful about the following:
The UIView you are trying to add the UITapGestureRecognizer has userInteractionEnabled set to true:
self.view.userInteractionEnabled = true
The UIView you are trying to get the tap to has no other views covering it. Use the View Debugger to confirm this.
Most importantly, make sure you are adding the UITapGestureRecognizer to the correct UIView. Adding it to self.view will add it to the UIViewController's view.
As a side note: You can add the UITapGestureRecognizer using the Inetrface Builder itself, then connecting IBAction for the same. Will reduce the probability of simple mistakes.

Recognize swipe gesture in view not in subview

I have added a subview to a View Controller's view. This subview is the view of QLPreviewController.
What I am trying to achieve is to recognize swipe gestures on the subview in the parent view, i.e. the View Controller's view. In the end, I want to be able to swipe left /right on the view to load the next document for preview.
I'm aware of hit testing and understand that by just attaching a gesture recognizer to the parent view, those will not be recognized, since the subview will be the "hit-test" view.
Now what is the best (or easiest) way to recognize those gestures?
Note: I didn't manage to attach the gesture recognizers to the subview, this doesn't seem to work.
* UPDATE *
To make this more clear - this is the code from my ViewController. vContent is just a view in my ViewController, where I add the view of the QLPreviewController:
let pvVc = QLPreviewController()
pvVc.dataSource = self
vContent.addSubview(pvVc.view)
I tried adding the swipe recognizers both to the vContent and the pvVc.view. In both cases no event was fired.
let sgrLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action:Selector("handleSwipe:"))
sgrLeft.direction = UISwipeGestureRecognizerDirection.Left
sgrLeft.delegate = self
On some other view the code works fine.
Any hint is appreciated!
Thx
Eau
Well, the responder chain, the unknown animal … ;-)
You can subclass the superview and override -hitTest:forEvent:.
You rarely need to call this method yourself, but you might override it to hide touch events from subviews.
Gesture Recognizers Get the First Opportunity to Recognize a Touch, so even the subview is hitTest view. the gestureRecognizer attached on superView can recognizer touch event.

Resources