1.創(chuàng)建UITabBarController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UIViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[firstViewController, secondViewController];
self.tabBarController.delegate = self; // 設(shè)置代理
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
2.設(shè)置代理
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSUInteger shouldSelectIndex = [tabBarController.viewControllers indexOfObject:viewController];
if (tabBarController.selectedIndex == shouldSelectIndex) {
return YES;
}
CATransition *animation = [CATransition animation];
animation.duration = 0.75;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionPush;
if (tabBarController.selectedIndex > shouldSelectIndex) {
animation.subtype = kCATransitionFromLeft;
} else {
animation.subtype = kCATransitionFromRight;
}
// 與百度上一般文章不一樣
[[[tabBarController valueForKey:@"_viewControllerTransitionView"] layer] addAnimation:animation forKey:@"animation"];
return YES;
}
3.區(qū)別
在百度的文章里狰闪,最后一句設(shè)置動(dòng)畫的語句為:
[tabBarController.view.layer addAnimation:animation forKey:@"reveal"];
這樣會(huì)導(dǎo)致tabBarController的整個(gè)標(biāo)簽欄也跟著一起有動(dòng)畫的效果环揽,這樣的效果并不是很好魏颓。
當(dāng)然動(dòng)畫的效果可以自己重新改寫啡直,記下以防自己忘記。