Ios – Setting up SwipeGestureRecognizer, clarification needed

iosobjective c

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

My Controller sets up

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

The GameView is set up

@interface GameView : UIView<UIGestureRecognizerDelegate> 

It further declares

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

and sets it up as

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

The Storyboard links UIGestureRecognizer with this IBACtion and is set up as follows:
enter image description here

I am a little concerned that UIGestureRecognizer appear underneath a Controller and not the view as shown, but i can't seem to be able to correct it.

enter image description here

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

Please suggest what i am missing here and whether i am going the right way about setting things up.

Best Answer

You are setting up two separate gesture recognizers. One in code which likely does nothing, the other in the storyboard that likely has no target.

In code you should initialize a swipe gesture recognizer like so:

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 could connect the swipe recognizer in the storyboard. This is easily done by either right clicking on the gesture recognizer and connecting the SentActions to the handleSwipeFrom: method, or similarly you can drag from the connections inspector on the right (as seen in screenshot) to the named function. In the image you can see I have my Sent Actions connected to the method swipeTarget:.

enter image description here

But currently you have two incomplete recognizers.

Related Topic