UI定制 - UITabBarController:轻扫则切换页面
■ 页面切换
1. 在开发中经常会遇到通过手机屏幕上向左或向右轻轻划动,就可以实现页面切换需求。下面我们就来实现这一功能
// - AppDelegate.m:指定根视图控制器
1 #import "AppDelegate.h" 2 #import "TabBarController.h"
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 3 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 4 self.window.backgroundColor = [UIColor whiteColor]; 5 [self.window makeKeyAndVisible]; 6 TabBarController *tbVC = [[TabBarController alloc] init]; 7 self.window.rootViewController = tbVC; 8 return YES; 9 }
// - TabBarController.m:里面存放 4 个控制器并,实现通过滑动进而切换页面功能
1 #import "TabBarController.h" 2 #import "FirstViewController.h" 3 #import "SecondViewController.h" 4 #import "ThirdViewController.h" 5 #import "FourthViewController.h" 6 @interface TabBarController ()<UITabBarControllerDelegate>// 代理 7 8 @end 9 10 @implementation TabBarController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 FirstViewController *firstVC = [[FirstViewController alloc] init]; 16 firstVC.title = @"微信"; 17 firstVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"微信" image:[UIImage imageNamed:@"mainframe@2x.png"] tag:100] ; 18 firstVC.tabBarItem.badgeValue = @"09"; 19 UINavigationController * navVC1 = [[UINavigationController alloc] initWithRootViewController:firstVC]; 20 navVC1.navigationBar.translucent = NO; 21 22 23 SecondViewController *secondVC = [[SecondViewController alloc] init]; 24 secondVC.title = @"指南"; 25 secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"指南" image:[UIImage imageNamed:@"discover@2x.png"] tag:101] ; 26 UINavigationController * navVC2 = [[UINavigationController alloc] initWithRootViewController:secondVC]; 27 navVC2.navigationBar.translucent = NO; 28 29 30 ThirdViewController *thirdVC = [[ThirdViewController alloc] init]; 31 thirdVC.title = @"列表"; 32 thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"列表"image:[UIImage imageNamed:@"more_hl@2x.png"] tag:102]; 33 UINavigationController * navVC3 = [[UINavigationController alloc] initWithRootViewController:thirdVC]; 34 navVC3.navigationBar.translucent = NO; 35 36 37 FourthViewController *fourthVC = [[FourthViewController alloc] init]; 38 fourthVC.title = @"警告"; 39 fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"警告" image:[UIImage imageNamed:@"qr_toolbar_light_up_hl@2x.png"] tag:103]; 40 UINavigationController * navVC4 = [[UINavigationController alloc] initWithRootViewController:fourthVC]; 41 navVC4.navigationBar.translucent = NO; 42 43 self.delegate = self; 44 self.viewControllers = @[navVC1,navVC2,navVC3,navVC4]; 45 self.tabBar.tintColor = [UIColor redColor]; 46 self.tabBar.translucent = NO; 47 48 // 添加手势:滑动 49 [self makeSwitchover]; 50 51 } 52 53 54 - (void)makeSwitchover{ 55 56 UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRightButton)]; 57 [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 58 [self.view addGestureRecognizer:swipeLeft]; 59 60 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton)]; 61 [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 62 [self.view addGestureRecognizer:swipeRight]; 63 } 64 65 // 右扫 66 -(void)tappedRightButton{ 67 68 NSUInteger selectedIndex = [self selectedIndex]; 69 NSArray *aryViewController = self.viewControllers; 70 71 if (selectedIndex < aryViewController.count - 1) { 72 UIView *fromView = [self.selectedViewController view]; 73 UIView *toView = [[self.viewControllers objectAtIndex:selectedIndex + 1] view]; 74 [UIView transitionFromView:fromView toView:toView duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) { 75 76 if (finished) { 77 78 [self setSelectedIndex:selectedIndex + 1]; 79 } 80 81 }]; 82 } 83 } 84 85 // 左扫 86 -(void)tappedLeftButton{ 87 88 NSUInteger selectedIndex = [self selectedIndex]; 89 if (selectedIndex > 0) { 90 91 UIView *fromView = [self.selectedViewController view]; 92 UIView *toView = [[self.viewControllers objectAtIndex:selectedIndex - 1] view]; 93 [UIView transitionFromView:fromView toView:toView duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) { 94 95 if (finished) { 96 97 [self setSelectedIndex:selectedIndex - 1]; 98 } 99 100 }]; 101 } 102 } 103 104 @end
// - FirstViewController.m
1 #import "FirstViewController.h" 2 @implementation FirstViewController 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 7 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -self.tabBarController.tabBar.frame.size.height - 64)]; 8 imageView.image = [UIImage imageNamed:@"微信.jpeg"]; 9 [self.view addSubview:imageView]; 10 } 11 12 13 @end
// - SecondViewController.m
1 #import "SecondViewController.h" 2 @implementation SecondViewController 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -self.tabBarController.tabBar.frame.size.height - 64)]; 7 imageView.image = [UIImage imageNamed:@"指南.jpeg"]; 8 [self.view addSubview:imageView]; 9 10 } 11 12 @end
// - ThirdViewController.m
1 #import "ThirdViewController.h" 2 @implementation ThirdViewController 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 7 UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -self.tabBarController.tabBar.frame.size.height - 64)]; 8 imageView.image = [UIImage imageNamed:@"列表.jpeg"]; 9 [self.view addSubview:imageView]; 10 } 11 12 @end
// - FourthViewController.m
1 #import "FourthViewController.h" 2 @implementation FourthViewController 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -self.tabBarController.tabBar.frame.size.height - 64)]; 7 imageView.image = [UIImage imageNamed:@"警告.jpeg"]; 8 [self.view addSubview:imageView]; 9 } 10 11 @end
运行效果