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");
}
}
Similar questions here and here.
The parameter to your
swipedScreen:method is of typeUISwipeGestureRecognizeri.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 thedirectionproperty of this recognizer to be(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)– this will not have changed.You will have to create two recognizers, one for each direction.