0

I'm trying to only detect a right swipe with a UISwipeGestureRecognizer. Even though I have set it to "Right", it's still registering swipes in both directions. I've searched online and read all of the dev pages Apple provides. I'm not seeing a way to successfully do this.

As requested:

.h:

IBOutlet UISwipeGestureRecognizer *decade1;
@property (nonatomic, retain) UISwipeGestureRecognizer *decade1;

.m:

@synthesize decade1;

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{
    if((page == 4) && (decade1))
{

        printf("HELLO!!");
    }
}
Jackson Egan
  • 2,715
  • 4
  • 18
  • 26
  • Please post your UISwipeGestureRecognizer init code. – Andrei G. Jul 21 '12 at 04:00
  • Done. This isn't all of the app code, but you get the idea. It's printing correctly on "page == 4". It's just that it's printing in both directions. I've tried several different ways of expressing the "if" statement to no avail. – Jackson Egan Jul 21 '12 at 04:05
  • You did this in IB... well as long as you have a reference to it (iVar), set its `direction` bitmask. – CodaFi Jul 21 '12 at 04:09
  • @CodaFi I think I understand what you're saying, but if you have time to elaborate, I'd appreciate it. I thought I did that in IB when I set it "Right". – Jackson Egan Jul 21 '12 at 04:24

1 Answers1

2

Create gesture recognizer like this, and add it to the view that you want it on:

UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
[rightSwipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:rightSwipeGestureRecognizer];

define a method to handle the swipe:

-(void)rightSwipe:(UISwipeGestureRecognizer*)recognizer {

    NSLog(@"Right swipe");

}

if it is still does not work, try NOT adding the recognizer to the view and see if there is another recognizer that is active..

EDIT (after seeing your code):

you have your logic inside the scrollViewDidScroll method, that is why it is reacting to any type of swipe (scroll actually). I doubt your gesture recognizer even works in this type of setup.

Gesture recognizer needs to be added to a view, then you need to connect a method that will run when gesture is recognized. Keep in mind that if you set direction of the gesturerecognizer to right, the method should only get called after right swipe. I would review the class again, to gain full understanding on how it works. UIGestureRecognizer Class Reference

Since you are using a UIScrollView, it is a little bit more tricky to recognize a swipe, since it has it's own recognizers in place. have a look at How to recognize swipe gesture in UIScrollView

I would also suggest keeping it simple, and adding the UISwipeGestureRecognizer programmatically (most likely you don't even need to declare an instance variable for it, as the object will be returned to the @selector function on the recognizer.

Community
  • 1
  • 1
Andrei G.
  • 1,710
  • 1
  • 14
  • 23
  • Hmmmm...will try immediately. – Jackson Egan Jul 21 '12 at 04:10
  • I'm getting the same result, which leads me to believe that the "if" statement is only reacting to the "page == 4" condition. This makes sense, but I'm not sure why the other condition isn't being considered. Any thoughts? – Jackson Egan Jul 21 '12 at 04:18
  • You know that if statement is only checking for the _existence_ of the swipe gesture recognizer, not the actual direction, right? – CodaFi Jul 21 '12 at 04:25
  • I realize that, but I can't seem to get it to check the direction or to have the recognizer return TRUE only in the event of a right swipe. – Jackson Egan Jul 21 '12 at 04:30