iOS UITabBarController自定義轉場動畫
最近找UITabBarController自定義轉場動畫方面的資料看到了這篇博客个榕,我現(xiàn)在把實踐過程中的思路和遇到的問題分享出來案疲。
iOS開發(fā)中經常需要自定義轉場動畫的場景有以下幾個
- UINavigationController的Push和Pop操作
- UIViewController的Present和Dismiss操作
- UICollectionViewController點擊cell轉場動畫
- UITabBarController的切換子控制器的時候
本篇就說說很簡單的UITabBarController的自定義轉場動畫。在此之前大家可以看看唐巧老師的這篇博客耘分。
iOS 7 以協(xié)議的方式開放了自定義轉場的 API,協(xié)議的好處是不再拘泥于具體的某個類绑警,只要是遵守該協(xié)議的對象都能參與轉場求泰,非常靈活。轉場協(xié)議由5種協(xié)議組成计盒,在實際中只需要我們提供其中的兩個或三個便能實現(xiàn)絕大部分的轉場動畫:
1.轉場代理(Transition Delegate)
2.動畫控制器(Animation Controller)
3.交互控制器(Interaction Controller)
4.轉場環(huán)境(Transition Context)
5.轉場協(xié)調器(Transition Coordinator)
總結下渴频,5個協(xié)議只需要我們操心3個;實現(xiàn)一個最低限度可用的轉場動畫北启,我們只需要提供上面五個組件里的兩個:轉場代理和動畫控制器即可卜朗,還有一個轉場環(huán)境是必需的,不過這由系統(tǒng)提供咕村;當進一步實現(xiàn)交互轉場時场钉,還需要我們提供交互控制器,也有現(xiàn)成的類供我們使用懈涛。
完成最基本的逛万、可用的UITabBarController轉場只需留意兩個協(xié)議(UITabBarControllerDelegate,UIViewControllerAnimatedTransitioning)即可。以下是我寫這個小demo的思路過程批钠。
1.搭建起最稀疏平常的TabBarVC/NavVC/VC結構
在開篇提到的那篇博客中宇植,作者把TabBarVC的代理設置為TabBarVC自己。個人覺得這樣做不好埋心。協(xié)議(代理)/KVO/Block/通知 是OC中類與類通信的四個基本方法指郁,它們都可以解耦代碼。我選擇新建一個Transform類去做TabBarVC的代理踩窖。
2.Transform類
.h文件內遵守協(xié)議坡氯。preIndex和selectedIndex是為了后面判斷TabBarVC的控制器切換方向而設置。后面再講洋腮。
#import <UIKit/UIKit.h>
@interface Transformer : NSObject <UITabBarControllerDelegate,
UIViewControllerAnimatedTransitioning>
@property (assign, nonatomic) NSInteger preIndex;
@property (assign, nonatomic) NSInteger selectedIndex;
@end
最低完成度的TabBar轉場動畫箫柳,在UITabBarControllerDelegate只需實現(xiàn)下面這個方法。這個方法返回一個動畫控制器(任何遵守UIViewControllerAnimatedTransitioning啥供,并實現(xiàn)關鍵方法的對象)悯恍。蘋果習慣在協(xié)議的代理方法中,把自己作為參數(shù)傳給其他類伙狐。這個做法值得咱們效仿涮毫。
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
animationControllerForTransitionFromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
UIViewControllerAnimatedTransitioning里面必須實現(xiàn)的兩個方法瞬欧,第一個方法告訴系統(tǒng)轉場動畫的執(zhí)行時間,并提供了一個轉場上下文(任何遵守UIViewControllerContextTransitioning的對象)作為參數(shù)罢防,通過這個上下文參數(shù)我們能獲取很多有用的信息艘虎。第二個方法不返回任何值,大家就知道這個方法里就是寫動畫代碼了咒吐。
// This is used for percent driven interactive transitions, as well as for
// container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
.m文件初始化Transform對象
//動畫參數(shù)
static CGFloat const kPadding = 10;
static CGFloat const kDamping = 0.75;
static CGFloat const kVelocity = 2;
- (instancetype)init
{
self = [super init];
if (self) {
_preIndex = 0;
_selectedIndex = 0;
}
return self;
}
.m文件實現(xiàn)UITabBarControllerDelegate的方法
//UITabBarControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
ByTabBarController *tabVC = (ByTabBarController *)tabBarController;
return tabVC.transform;
}
注意野建,后面為了判斷TabBarVC的滑動方法,都需要去獲取Transform實例對象的preIndex和selectedIndex屬性恬叹。我最初在這個方法中每次都拋出一個新創(chuàng)建的Transform對象候生,也就是直接return一個[Transform new],導致每次獲取到的preIndex和selectedIndex都是初始值绽昼。這里我把Transform實例對象設置為TabBarVC的一個屬性唯鸭,并放在.h中暴露出來。
.m文件實現(xiàn)UIViewControllerAnimatedTransitioning的方法
//UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return 0.33;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
NSLog(@"Animation begin... ... ");
//1.
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *containerView = [transitionContext containerView];
CGFloat translation = containerView.bounds.size.width + kPadding;
CGAffineTransform transform = CGAffineTransformMakeTranslation(_preIndex > _selectedIndex ? translation : -translation, 0);
toViewController.view.transform = CGAffineTransformInvert(transform);
[containerView addSubview:toViewController.view];
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:kDamping initialSpringVelocity:kVelocity options:UIViewAnimationOptionCurveEaseInOut animations:^{
fromViewController.view.transform = transform;
toViewController.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
fromViewController.view.transform = CGAffineTransformIdentity;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
3.判斷TabBarVC的滑動方向
a.給UITabBar的每個TabBarItem的tag賦值
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
for (NSInteger i = 0; i < self.childViewControllers.count; i++) {
[self.tabBar.items[i] setTag:i];
}
}
b.在UITabBar的代理方法中給Transform對象的preIndex和selectedIndex賦值
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
self.transform.selectedIndex = item.tag;
self.transform.preIndex = self.selectedIndex;
}
因為UITabBar的代理方法先與UITabBarViewController的代理方法調用硅确。所以在tabBar:
didSelectItem:方法中目溉,item的tag值是當前被選中的VC的index,而TabBarVC的selectedIndex屬性保留的任然是之前的VC的index疏魏。
demo地址在這兒停做。