Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up SwipeGestureRecognizer, clarification needed

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.

like image 358
James Raitsev Avatar asked Mar 14 '12 01:03

James Raitsev


3 Answers

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.

like image 128
NJones Avatar answered Sep 27 '22 18:09

NJones


-(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;
}
like image 44
Vibhooti Avatar answered Sep 27 '22 20:09

Vibhooti


    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 it in code, then do it in IB too you're just duplicating work, and maybe doubling the times the swipe is handled

like image 34
Brodie Avatar answered Sep 27 '22 18:09

Brodie