Disabling UIGestureRecognizers in underlying ViewController - ios

In a View Controller I'm building a grid of icons.
Each icon opens the same pop-up view , but filled in with different information.
I'm creating the grid in this way:
for (int i=0; i<NUM_BADGES; i++) {
BadgeThumbView *thumb = [[BadgeThumbView alloc] initWithFrame:CGRectMake(posX, posY, 70, 100)
andWithLabel:[NSString stringWithFormat:#"BADGE NAME N. %d", i]];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onBadgeTapped:)];
[thumb addGestureRecognizer:gestureRecognizer];
[thumb setTag:i];
[more code here....]
}
And in onBadgeTapped method I'm creating the pop up.
Now my problem is that everything works fine, but I just realized that when the pop up is opened, while interacting with its buttons I'm still triggering the gesture recognizer in the underlying view controller.
Is there a way to disable all GestureRecognizers in underlying view? Is my strategy wrong? And: is there a way to use a single UIGestureRecognizer for all my icons, in order to disable/enable in a easier way?
Thanks a lot

You can remove the recognizer from view or set userInteractionEnabled to temporarily disable it. Depending on how your popup is implemented, you may be able to disable them all at once.
One solution would to be adding thumbs as a subview of a container UIView, and adding that container to your parent view. You could then enable/disable all by setting userInteractionEnabled on the container view.

I think you should do some thing like disabling the userInteraction for all thumb views when the popup is appearing and re-enabling when disappearing like this
[[yourSuperView subviews]makeObjectsPerformSelector:#selector(setUserInteractionEnabled:) withObject:[NSNumber numberWithBool:FALSE]];
Else add all thumbViews to one subview( say 'b') then add view 'b' to superview(say 'a') as subView and turn off the user interaction to view b when popup appeared and turn on when popup disappears

Related

SubView not Considered a part of MainView

I have many SubViews in my UIView, and many of them have UIButtons in them. One of the subviews- _bottomView (Coordinates- (0,519,320,49)) has an error. It does not recognise the click events on the buttons placed inside it.
I tried placing a UIButton covering the entire _bottomView and the click event from that Button (testButton) is not being recognised either.
I tried adding a tapRecogniser to the code and the tap from every point, EXCEPT the points within the _bottomView are recognised. TapRecogniser Code below
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
_bottomView.userInteractionEnabled=true;
-(void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.view];
NSLog(#"got a tap in the region i care about");
}
I tried [self.view addSubview:_bottomView]; and that didn't help either. What could be the issue?
If your view is not receiving touches
Make sure that your UIView has userInteractionEnabled set to YES
Make sure that the view is within the bounds of its superview, and the superview is within the bounds of window.You can print the frames of views using NSLog(#"%#",NSStringFromCGRect(view.frame));
Nothing helped. I had to delete the entire View Controller and redo the whole thing. That obviously solved the problem.

Accessing the Status Bar

So, I'd like to receive touches from the status bar anywhere, any view, from the app. Well actually only specific views, like home screens and root view controllers say from a view of a tab view controller.
So my goal is to: hold down the status bar to dismiss the current view, like a back button.
The reason why I'm using this approach as a back button is because the app I am making is a utility, like fully functional apps within one app. Therefore I do not want to use up space on the navbar item to display a back button.
Other fissures would be nice, like swipe left/right or double tap, but mostly holding down. I've seen alternatives like using a UIScrollView and use the - scrollsToTop method. Hmm not what I am looking for.
Hope this can help you.
When you tap the top area, which has 44 height, the event would be triggered.
- (void)viewDidLoad{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(navigationBack:)];
[self.view addGestureRecognizer:tapGesture];
}
- (void)navigationBack:(UITapGestureRecognizer *)tapGesture {
CGPoint p = [tapGesture locationInView:self.view];
if (CGRectContainsPoint(CGRectMake(0, 0, self.view.frame.size.width, 44), p)) {
NSLog(#"this where you put you navigation back event");
}
}

Accessibility label wih iOS ViewController presented as a form sheet

I am making use of a Presentation: Form Sheet in my app. So my ViewController gets displayed as a form with the black semi-transparent overlay around it.
I have implemented the logic to dismiss everything should the user tap anywhere outside my form sheet ViewController.
I would like to test this behavior but I'm not sure how to simulate the tap. How can I set an accessibility label to simulate this tap with a UI test?
Or any other suggestions how I can test this behavior?
Thanks!
You just want to click anywhere on the screen to dismiss everything?
[tester tapScreenAtPoint:CGPoint];
does that for you.
Most stuff about KIF is explained here:
http://www.raywenderlich.com/61419/ios-ui-testing-with-kif
Hi you can use UITapGestureRecognizer like this :
First create an instance of UITapGestureRecognizer
UITapGestureRecognizer *tapGesture = tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(someMethod:)];
Then attach this gesture recognizer to your view (i.e, black overlay you're talking about )
[self.view addGestureRecognizer:tapGesture];
Then implement someMethod: which is the method (action) that gets called when your formsheet is tapped
-(void)someMethod
{
//Logic to dismiss your formsheet
}
HTH :D

Set exclusive touch on multiple UIViews of the same class

I am creating a random number of custom UIViews of the same class, and I'm adding them in the UIViewController's view. I'm assigning them a UITapGestureRecognizer, but I can't seem to make the exclusive touch work:
for (int i = 0; i <= n; i++) {
ICCatalogProductView *catalogProductView;
catalogProductView = [[ICCatalogProductView alloc] init];
[self.view addSubview:catalogProductView]
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(testTouch)];
[catalogProductView addGestureRecognizer:tapGesture];
[catalogProductView setExclusiveTouch:YES];
}
If i tap the UIViews simultanously, the method is called twice (not the behaviour I want). Is there any elegant method of solving this, or any method at all?
From the Apple Documentation:
exclusiveTouch only prevents touches in other views during the time in
which there's an active touch in the exclusive touch view. That is, if
you put a finger down in an exclusive touch view touches won't start
in other views until you lift the first finger. It does not prevent
touches from starting in other views if there are currently no touches
in the exclusiveTouch view.
To truly make this view the only thing on screen that can receive
touches you'd need to either add another view over top of everything
else to catch the rest of the touches, or subclass a view somewhere in
your hierarchy (or your UIWindow itself) and override
hitTest:withEvent: to always return your text view when it's visible,
or to return nil for touches not in your text view.
means its only set exclusive in your one view, not if you are touching something outside your view.

Detecting touches with stacked Views & passing Touch events from a UIScrollView

I'm trying to close a pop-up menu if the user selects anywhere EXCEPT with-in the bounds of the pop-up itself. In other words, they select the background, the pop-up closes.
I currently have a RootViewController that loads a UIView with a bunch of other UI Elements.
At one point, a pop-up (custom) menu is rendered - this is a separate UIViewController with associated .xib. Its loaded on the screen by:
SectionsChooserViewController *cv = [[SectionsChooserViewController alloc] initWithNibName:#"SectionsChooserViewController" bundle:[NSBundle mainBundle]];
cv.view.frame = CGRectMake(584, 391, 314, 346);
cv.delegate = self;
This view occupies only a small portion of the screen. So added detect code for touches to this view will only register the callback if you're ON the view.
If I don't handle touches in the "SectionsChooserViewController" and instead implement the touchesBegan in the rootViewController (which loads the "SectionsChooser...") it doesn't get called for 90% of the view because the UIScrollView's that I have seem to register the touch and not pass it on.
Is there a way to make the UIScrollView pass the touch event?
Edit: At least I think the UIScrollView's are taking the touch event. I'm not sure - I have nearly 20 UI Controls on the page.
Edit: Ok, it is the UIScrollView. If I turn off interaction-enabled on both scrollView's the UIView will get the touches, but this breaks the UIScrollViews - so that won't work.
You might be able to use [super touchesBegan:withEvent:] to help solve the problem.
Depending on what your trying to do you might be able to use UIGestureRecognizer to capture the touch events.
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleDoubleTap:)];
doubleTapGestureRecognizer.numberOfTapsRequired = 2; //change value to 1 for single taps
[self addGestureRecognizer:doubleTapGestureRecognizer];

Resources