How to block a gesture from a supervisor to a peek?

I am writing a module that every time I sit down on a view, two sub-views with half the size of the view will be added. These subspecies have their own gestures (for example: panning, ...). The first time I scroll, this is normal because none of the subviews are created. But as soon as the subview was created, every time I sit down, with a gesture, the swipe always goes to its subzones. :(, so I have to scroll 2 times to split.

I want to know if there is a way to block napkins passing into the subtitle? Thanks.

UPDATE
I used shouldRecognizeSimultaneousWithGestureRecognizer so that these actions work simultaneously. But there are still problems. The parent view has its own Swipe gesture, subview has its Pan gesture. Since I use souldRecognizeSimultaneousWithGestureRecognizer, sometime when I pan, it starts with a swipe gesture. So, do you know how to disable Swipe while Pan is active in this situation?

+6
source share
5 answers

You need to implement the UIGestureRecognizerDelegate method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer; 

And add your controller as a delegate of gesture recognizers. Then, when two gesture recognizers respond to the gesture, this method will be called, and here you can implement the logic that you want for your application.

In the controller interface declaration, you must enter:

 @interface testcViewController () <UIGestureRecognizerDelegate> 

Then, when creating the gesture recognizer:

 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)]; swipe.direction = UISwipeGestureRecognizerDirectionDown; swipe.delegate = self; [self.view addGestureRecognizer:swipe]; 

And then finally you add this method to the controller:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { BOOL shouldInteract = NO; //Here you decide whether or not the two recognizers whould interact. return shouldInteract; } 

EDIT You can also implement

 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer; 

And here, determine if you have already submitted sub-items, and block any gesture you want.

+12
source

set userinteractionEnabled to NO of your subView

  subview.userinteractionEnabled=NO 

If you do not want to disable userInteraction, use the cancelsTouchesInView method

cancels touchesInView. If the gesture recognizer recognizes its gesture, it repels the remaining strokes of this gesture from their point of view (so the window will not deliver them). The window cancels previously delivered touches using the message (touchsCancelled: withEvent :). If the gesture recognizer does not recognize its gesture, the view gets an all-touching sequence with a few touches.

+2
source

try it,

  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return NO; } 
0
source

To block all gesture recognizers from superviews, I created a subclass of the UIGestureRecognizer class that will only do this when connected to a view. See the following code (taken from my WEPopover project):

 #import "WEBlockingGestureRecognizer.h" #import <UIKit/UIGestureRecognizerSubclass.h> @implementation WEBlockingGestureRecognizer - (id)init { return [self initWithTarget:self action:@selector(__dummyAction)]; } - (id)initWithTarget:(id)target action:(SEL)action { if ((self = [super initWithTarget:target action:action])) { self.cancelsTouchesInView = NO; } return self; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ if (self.state == UIGestureRecognizerStatePossible) { self.state = UIGestureRecognizerStateBegan; } } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ self.state = UIGestureRecognizerStateRecognized; } - (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer { return [self isGestureRecognizerAllowed:preventingGestureRecognizer]; } - (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer { return ![self isGestureRecognizerAllowed:preventedGestureRecognizer]; } - (BOOL)shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return ![self isGestureRecognizerAllowed:otherGestureRecognizer]; } - (BOOL)shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return NO; } - (BOOL)isGestureRecognizerAllowed:(UIGestureRecognizer *)gr { return [gr.view isDescendantOfView:self.view]; } - (void)__dummyAction { } @end 

If you want to block all gesture recognizers attached to parent views of some kind, follow these steps:

 - (void)blockParentGesturesForView:(UIView *)v { [v addGestureRecognizer:[WEBlockingGestureRecognizer new]]; } 
0
source

Given that I have a dialogView as a direct subset of my UIViewController main view , I attach a gesture recognizer to the main view and do the following (setting my view controller as a delegate) gesture recognizer delegate) :

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { let point = touch.location(in: view) return !dialogView.frame.contains(point) } 
0
source

Source: https://habr.com/ru/post/946757/


All Articles