Tap Gesture Recognizer and UIControl - ios

I've a view controller with UISegmentedControl and UITableView as its children. In -viewDidLoad I create a UITapGestureRecognizer and add it to the controller's main view.
When I tap on, for instance, a cell of the table view, the tap gesture recognizer's action is being called and it prevents receiving touches to the table view (that's ok as tapRecognizer.cancelsTouchesInView == YES).
However, the issue is that when I tap on the segmented control, the tap gesture recognizer's action is not being called. Why is it so? How to make the tap gesture recognizer the "top" object which handles touches?
Thanks,
Adam

Related

How to detect long press within a view and its subviews swift

I have a view (I'll call it parentView) that has about 20-30 subviews. I have added a long press gesture recognizer to the parentView. The gesture recognizer only seems to fire when I press on the parentView, it does not fire when I press and hold on one of the subviews.
I have tried adding the gesture recognizer to self.view and using the gesture location to see if it was within the bounds of the parentView. However the same problem occurs since it does not seem to detect my long press on the subviews.I have also tried running a for loop and adding the gesture recognizer to each individual subview but this also did not work.
This is how I am defining my gesture recognizer if anybody was wondering.
longPress.minimumPressDuration = 1
longPress.addTarget(self, action: #selector(ViewController.handleLongPress)
parentView.addGestureRecognizer(longPress)
How would I get the long press gesture recognizer to detect a long press on the parentView and it's subviews?

UITableView cancels UITapGesture

uitableviewdidselect cancel outs tap gesture action
there is an UIImageView in cell.contentView and there is a tap gesture to enlarge the image the control is not going to the Tap Gesture action its passing to the tableview didselect delegate ? I am using UITableView class for my table already did userInteractionEnabled=YES & cancelsTouchesInView = NO
Make sure you set
tapGestureRecognizer.cancelsTouchesInView = NO;
It won't work because table view can't take two user interactions at a time. First it will give priority to didSelectRowAtIndexPath. You need to disable user interaction on cells. So that tap gesture will be called. Make sure that user interaction enabled for imageView.

Single tap gesture to multiple View using storyboard

How to have single tap gesture to multiple Views using storyboard.
I drag 3 views and one tap gesture into UIView class.
Contacted three view to tap gesture and added action class handleGesture Method on tap on any one the tree view it should trigger the method action.
using story board.
But i want to do it with single tap gesture is it possible or not.
try with this, define a variable UITapGestureRecognizer with your method, after that, in a method or wherever you want, you can add this gesture to your multiple views
var recognizerMovements: UITapGestureRecognizer {
get { UITapGestureRecognizer(target: self, action: #selector(self.myActionMethod)) }
}
self.myFirstView.addGestureRecognizer(myActionMethod)
self.mySecondView.addGestureRecognizer(myActionMethod)

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.

UISplitViewController swipe gesture interferes with other swipe gesture

I'm using a UISplitViewController and one of the detail view controllers contains a view which has a UIPanGestureRecognizer added to it. When I swipe this view in the detail view controller, the gesture is recognized, but the swipe gesture recognizer of the split view controller interferes with it; the master view controller is displayed and the gesture recognizer from the detail controller is ignored.
Implementing and debugging the shouldRecognizeSimultaneouslyWithGestureRecognizer method from the UIGestureRecognizerDelegate shows two UIPanGestureRecognizer objects: one from the detail view controller and the one from the split view controller, so I'm certain they are interfering with each other.
When I set presentsWithGesture = NO on the split view controller, the gesture recognizer inside the detail view controller does work. But that disables the gesture recognizer on the split view controller, so it's not really a solution to the problem.
I've also tried disabling the gesture recognizer on the split view controller, only when I need the other gesture recognizer to work, but it seems that it is not possible to set presentsWithGesture once the split view controller has become visible.
I've also tried to disable the default gesture on the split view controller and adding a custom gesture which I can control, but it didn't work. I tried using the target and action from the split view controller barbutton on the gesture, but it doesn't work. Calling toggleMasterVisible: on the split view controller isn't an option either because it's part of the private api.
Does anyone have any suggestions on how to handle this?
I would suggest that you disable the UISplitViewController pan gesture when you need the other one to work. This should do it:
for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
[recognizer setEnabled:NO];
}
}
You probably don't want to search for it each time, so I would store a reference to that gesture recognizer when the view loads, then just disable and enable as appropriate:
on viewDidLoad:
for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
[self setSplitViewPanGesture:recognizer];
}
}
later:
[self.splitViewPanGesture setEnabled:NO];
and later:
[self.splitViewPanGesture setEnabled:YES];

Resources