Reputation: 31
I am new to Objective-C and Xcode and I am having difficulty in changing views. I understand you can use the UIScrollView
to swipe between image views, but I would like to simply swipe right to change views, and swipe left to return. I have AppDelegate.h
and AppDelegate.m
which have not been changed. ViewController.h
and ViewController.m
are given below:
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m:
#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//sets up gesture recognizer
UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeRightGesture];
swipeRightGesture.direction=UISwipeGestureRecognizerDirectionRight;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender
{
NSUInteger touches = sender.numberOfTouches;
if (touches == 2)
{
if (sender.state == UIGestureRecognizerStateEnded)
{
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
}
}
}
@end
I also have ViewController2.h
and ViewController2.m
which I have linked to the second view in storyboard along with a gesture that I have linked to the handleSwipeGesture
method above.
Upvotes: 2
Views: 2568
Reputation: 1225
- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender
does not need to be an IBAction since it is not hooked up to an IBOutlet. It should return void
instead.
Additionally, instead of checking the number of touches once the gesture has already been recognized, you can make two touches a requirement for it to be recognized at all, by using the line:
swipeRightGesture.numberOfTouchesRequired = 2;
Consequently, the method -(void)handleSwipeGesture
no longer needs to take an argument, so you can remove the colon from the line where you declare the gesture recognizer.
The final code might look similar to this:
UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture)];
swipeRightGesture.direction=UISwipeGestureRecognizerDirectionRight;
swipeRightGesture.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:swipeRightGesture];
...
...
- (void)handleSwipeGesture {
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
}
Upvotes: 1
Reputation: 461
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *oneFingerSwipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(oneFingerSwipeLeft:)] ;
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];
}
- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer
{
NSLog(@"Swiped left");
// Add your navigation Code Here
}
U can use this if u want to use single finger swipe. IF u want to add two finger Swipe add your
NSUInteger touches = sender.numberOfTouches;
if (touches == 2)
{
if (sender.state == UIGestureRecognizerStateEnded)
{
}
}
above code in the (void)onefingerSwipeLeft
method
Upvotes: 2