Configuring SwipeGestureRecognizer, clarification required

I am missing something simple here, please help me understand that.

My controller installs

UIGestureRecognizer *swipe = [[UIGestureRecognizer alloc] initWithTarget:gameView action:@selector(handleSwipeFrom:)]; [gameView addGestureRecognizer:swipe]; 

GameView configured

 @interface GameView : UIView<UIGestureRecognizerDelegate> 

Further announced

 - (IBAction) handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer; 

and sets it as

 - (IBAction)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer { NSLog(@" .............. Swipe detected!! ..................."); } 

The Storyboard links UIGestureRecognizer with this IBACtion and are configured as follows: enter image description here

I'm a little worried that the UIGestureRecognizer displayed under the controller, not with the view as shown, but I cannot fix it.

enter image description here

When the application is built, swipes, however, are not recognized.

Please offer me something that I don’t see here, and whether I will adjust the situation correctly.

+6
source share
3 answers

You set up two separate gesture recognizers. One in the code that probably does nothing, the other in the storyboard, which probably has no purpose.

In the code, you must initialize the gesture recognizer like this:

 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:gameView action:@selector(handleSwipeFrom:)]; // And assuming the "Up" direction in your screenshot is no accident swipe.direction = UISwipeGestureRecognizerDirectionUp; 

Or, of course, you can connect a napkin recognizer in the storyboard. This is easily done by right-clicking on the gesture recognizer and connecting SentActions to the handleSwipeFrom: method, or in the same way you can drag from the connection inspector on the right (as shown in the screenshot) to the named function. In the image, you see that I have Sent Actions associated with the swipeTarget: method.

enter image description here

But currently you have two incomplete recognizers.

+16
source
 -(void)addSwipeEvent:(UIView*)subView{ UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; recognizer.numberOfTouchesRequired = 1; recognizer.delegate = self; [subView addGestureRecognizer:recognizer]; [recognizer release]; UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft; leftRecognizer.numberOfTouchesRequired = 1; leftRecognizer.delegate = self; [subView addGestureRecognizer:leftRecognizer]; [leftRecognizer release]; UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; downRecognizer.direction=UISwipeGestureRecognizerDirectionDown; downRecognizer.numberOfTouchesRequired = 1; donwRecognizer.delegate = self; [subView addGestureRecognizer:downRecognizer]; [downRecognizer release]; UISwipeGestureRecognizer *upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; upRecognizer.direction=UISwipeGestureRecognizerDirectionUp; upRecognizer.numberOfTouchesRequired = 1; upRecognizer.delegate = self; [subView addGestureRecognizer:upRecognizer]; [upRecognizer release]; } - (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender { if ( sender.direction == UISwipeGestureRecognizerDirectionLeft ){ NSLog(@" *** SWIPE LEFT ***"); } if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){ NSLog(@" *** SWIPE RIGHT ***"); } if ( sender.direction== UISwipeGestureRecognizerDirectionUp ){ NSLog(@" *** SWIPE UP ***"); } if ( sender.direction == UISwipeGestureRecognizerDirectionDown ){ NSLog(@" *** SWIPE DOWN ***"); } } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([touch.view isKindOfClass:[UIView class]]) { return YES; } return NO; } 
+11
source
  UISwipeGestureRecognizer *swipe = [[UISwipGestureRecognizer alloc] initWithTarget:gameView action:@selector(handleSwipeFrom:)]; [gameView addGestureRecognizer:swipe]; - (IBAction)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer { NSLog(@" .............. Swipe detected!! ..................."); } 

Then just undo what you did in IB. If you do this in code, then do it in IB too, you just duplicate the work and, possibly, double the processing time of the napkin

+1
source

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


All Articles