1

I am having some trouble with my UIView transition method that is listening for gestures on the screen.

Whats happening is, if I do a left swipe or a right swipe its sending a left and right swipe signal to my @selector method.. meaning I cannot differentiate between the swipes.

Here's my code in question.. I have tried a few different things, but cannot seem to get this one right.

- (void) setupSwipeGestureRecognizer {
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen:)];
        swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
        [self.view addGestureRecognizer:swipeGesture];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Prototype";
    //Initalizse the swipe gestuer listener
    [self setupSwipeGestureRecognizer];

    //alloc and init
    self.detailViewA = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; 
    self.detailViewB = [[DetailViewControllerB alloc]initWithNibName:@"DetailViewControllerB" bundle:[NSBundle mainBundle]];

    // set detail View as first view
    [self.view addSubview:self.detailViewA.view];

    // set up other views
    [self.detailViewB.view setAlpha:1.0f];

    // Add the view controllers view as a subview
    [self.view addSubview:self.detailViewB.view];

    // set these views off screen (right)
    [self.detailViewB.view setFrame:CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)];    
}


- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture
{
    if (gesture.direction = UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left");
    }
   if (gesture.direction = UISwipeGestureRecognizerDirectionRight){
       NSLog(@"Right");
   }

}
Midas
  • 930
  • 2
  • 8
  • 20
C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • 2
    I think you have to create separate recognizers for each direction - have a look at [Setting direction for UISwipeGestureRecognizer](http://stackoverflow.com/questions/3319209/setting-direction-for-uiswipegesturerecognizer). – David Jones - iPushPull May 30 '12 at 08:20
  • Okay so I just ended up doing this... not sure if its the best solution but its defiantly working.. – C.Johns May 30 '12 at 10:03

4 Answers4

3

Similar questions here and here.

The parameter to your swipedScreen: method is of type UISwipeGestureRecognizer i.e. the recognizer that caused the callback to be called. It does not refer to any actual gesture that the user made. In your case you set the direction property of this recognizer to be (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight) - this will not have changed.

You will have to create two recognizers, one for each direction.

Community
  • 1
  • 1
David Jones - iPushPull
  • 2,819
  • 1
  • 22
  • 24
2

Try this code:

(void)swipedScreen:(UISwipeGestureRecognizer*)gesture {
    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left");  
    }
    if(gesture.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right");
    } 
}
Botz3000
  • 39,020
  • 8
  • 103
  • 127
iOS Test
  • 1,103
  • 1
  • 11
  • 18
  • nope, this doesn't work unfortunately. I have just debugged it and the thread enters the method, but bounces right over both if statements... however if it is simply = then it enters both if statements. – C.Johns May 30 '12 at 09:58
  • 1
    PLease create two seprate swipegesture for each view. and specify direction left for first one and right to second one, use the same selector for both gesture objects – iOS Test May 30 '12 at 10:07
1

Swift 3 version code:

func setupSwipeGestureRecognizer() {

        //For left swipe
        let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen))
        swipeGestureLeft.direction = .left
        self.view.addGestureRecognizer(swipeGestureLeft)

        //For right swipe
        let swipeGestureRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen))
        swipeGestureRight.direction = .right
        self.view.addGestureRecognizer(swipeGestureRight)

    }

override func viewDidLoad() {
        super.viewDidLoad()
     setupSwipeGestureRecognizer() 
}

func swipedScreen(gesture: UISwipeGestureRecognizer) {

        if gesture.direction == .left {
            print("Left")
        } else if gesture.direction == .right {
            print("Right")
        }
    }
mriaz0011
  • 1,887
  • 23
  • 11
0

You have not compared your if conditions correctly. It should have == as a comparator operator . You are using = which makes your condition always true.

Apurv
  • 17,116
  • 8
  • 51
  • 67
  • hrmm... that dose not seem to work at all using the comparison operator == it dosn't enter either if statement. – C.Johns May 30 '12 at 08:33
  • this is the project I am working with http://cl.ly/Gzaa I havent changed the if statments in that but if you wanna try you can.. the project I have open at the moment I have changed them and its not entering the if statments with the == – C.Johns May 30 '12 at 08:34