侧滑翻页
一: 工程图
二: 代码
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 //加入头文件 3 #import "PPRevealSideViewController.h" 4 5 @interface AppDelegate : UIResponder <UIApplicationDelegate,PPRevealSideViewControllerDelegate> 6 7 @property (strong, nonatomic) UIWindow *window; 8 9 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "MainViewController.h" 3 4 @implementation AppDelegate 5 6 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 7 { 8 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 9 // Override point for customization after application launch. 10 11 MainViewController *main = [[MainViewController alloc] init]; 12 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main]; 13 PPRevealSideViewController *revealSideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:nav]; 14 revealSideViewController.delegate = self; 15 self.window.rootViewController = revealSideViewController; 16 17 self.window.backgroundColor = [UIColor whiteColor]; 18 [self.window makeKeyAndVisible]; 19 return YES; 20 }
MainViewController.h
1 #import <UIKit/UIKit.h> 2 @interface MainViewController : UIViewController 3 4 @end
MainViewController.m
1 #import "MainViewController.h" 2 //加入头文件 3 #import "PPRevealSideViewController.h" 4 #import "leftViewController.h" 5 #import "rightViewController.h" 6 7 @interface MainViewController () 8 9 @end 10 11 @implementation MainViewController 12 13 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 14 { 15 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 16 if (self) { 17 // Custom initialization 18 } 19 return self; 20 } 21 22 - (void)viewDidLoad 23 { 24 [super viewDidLoad]; 25 // Do any additional setup after loading the view. 26 27 //设置背景色 28 self.view.backgroundColor= [UIColor orangeColor]; 29 30 //隐藏导航条 31 self.navigationController.navigationBarHidden=YES; 32 33 // 手势左右滑动屏幕 34 UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleMoveFrom:)]; 35 [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 36 [self.view addGestureRecognizer:swipeLeft]; 37 38 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleMoveFrom:)]; 39 [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 40 [self.view addGestureRecognizer:swipeRight]; 41 42 } 43 // 滑动事件 44 -(void)handleMoveFrom:(UISwipeGestureRecognizer *)swipe 45 { 46 if(swipe.direction == UISwipeGestureRecognizerDirectionRight){ 47 48 leftViewController *left = [[leftViewController alloc] init]; 49 [self.revealSideViewController pushViewController:left onDirection:PPRevealSideDirectionLeft withOffset:50.0 animated:YES]; 50 } 51 if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){ 52 rightViewController *right = [[rightViewController alloc] init]; 53 [self.revealSideViewController pushViewController:right onDirection:PPRevealSideDirectionRight withOffset:50.0 animated:YES]; 54 } 55 } 56 - (void)didReceiveMemoryWarning 57 { 58 [super didReceiveMemoryWarning]; 59 // Dispose of any resources that can be recreated. 60 }
rightViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface rightViewController : UIViewController 4 5 @end
rightViewController.m
1 #import "rightViewController.h" 2 3 @interface rightViewController () 4 5 @end 6 7 @implementation rightViewController 8 9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 10 { 11 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 12 if (self) { 13 // Custom initialization 14 } 15 return self; 16 } 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view. 22 23 //设置标题 24 self.title=@"right"; 25 //设置背景色 26 self.view.backgroundColor=[UIColor blueColor]; 27 } 28 29 - (void)didReceiveMemoryWarning 30 { 31 [super didReceiveMemoryWarning]; 32 // Dispose of any resources that can be recreated. 33 }
leftViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface leftViewController : UIViewController 4 5 @end
leftViewController.m
1 #import "leftViewController.h" 2 3 @interface leftViewController () 4 5 @end 6 7 @implementation leftViewController 8 9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 10 { 11 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 12 if (self) { 13 // Custom initialization 14 } 15 return self; 16 } 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view. 22 23 //设置标题 24 self.title=@"left"; 25 //设置背景色 26 self.view.backgroundColor=[UIColor redColor]; 27 28 } 29 30 - (void)didReceiveMemoryWarning 31 { 32 [super didReceiveMemoryWarning]; 33 // Dispose of any resources that can be recreated. 34 }