轉(zhuǎn)場(chǎng)動(dòng)畫(huà)代碼下載主要是tabBar痒给、Navi说墨、present ;其實(shí)它里面設(shè)置到了 主要涉及到了
動(dòng)畫(huà)
苍柏、交互
1 . TabBar的切換
tabbarVC 里面本身含有
<UITabBarControllerDelegate>
這個(gè)代理尼斧,這個(gè)時(shí)候我們定義一個(gè)id<UITabBarControllerDelegate>
類(lèi)型的實(shí)例MyTransitionTabbarDelegateManager
是現(xiàn)實(shí)里面的 動(dòng)畫(huà)和交互,讓tabBarVC的delegate
=MyTransitionTabbarDelegateManager
對(duì)象
下面是代碼實(shí)現(xiàn)
MyTransitionTabbarDelegateManager
#import "MyTransitionTabbarDelegateManager.h"
#import "MyTransitionTabBarAnimator.h"
@interface MyTransitionTabbarDelegateManager ()
@property (nonatomic,strong) UIPercentDrivenInteractiveTransition * interactiveTransition;
@end
@implementation MyTransitionTabbarDelegateManager
- (instancetype)init {
if (self = [super init]) {
self.interactiveTransition = [[UIPercentDrivenInteractiveTransition alloc]init];
self.interactive = NO;
}
return self;
}
//切換動(dòng)畫(huà)
- (id<UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
NSInteger fromIndex = [tabBarController.viewControllers indexOfObject:fromVC];
NSInteger toIndex = [tabBarController.viewControllers indexOfObject:toVC];
NSLog(@"from:%@;to:%@",fromVC,toVC);
/* 確定方向 **/
MyTransitionTabBarAnimator * animator = [[MyTransitionTabBarAnimator alloc]init];
animator.isSlideToRight = fromIndex > toIndex?YES:NO;
return animator;
}
//交互動(dòng)畫(huà)
- (id<UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
if (self.interactive) {
return self.interactiveTransition;
}
return nil;
}
@end
交互主要是 通過(guò)繼承 UITabBarController 來(lái)獲得一個(gè)子類(lèi) 然后在里面添加手勢(shì)
#import "MyScrollTabBarController.h"
#import "MyTransitionTabbarDelegateManager.h"
@interface MyScrollTabBarController ()
/* 處理 頁(yè)面交換 和 動(dòng)畫(huà) **/
@property (nonatomic,strong) MyTransitionTabbarDelegateManager * tabBarDelegateManager;
@end
@implementation MyScrollTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tabBarDelegateManager = [[MyTransitionTabbarDelegateManager alloc]init];
self.delegate = self.tabBarDelegateManager;
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
NSInteger i = 0;
for (UIViewController * vc in self.viewControllers) {
NSLog(@"vc_%ld:%@",(long)i++,vc);
}
}
#pragma mark - 私有方法
- (void)panAction:(UIPanGestureRecognizer *)pan {
CGFloat translationX = [pan translationInView:self.view].x;
CGFloat progress = fabs(translationX)/CGRectGetWidth(self.view.frame);
switch (pan.state) {
case UIGestureRecognizerStateBegan: {
self.tabBarDelegateManager.interactive = YES;
/*
*根據(jù)pan的速度 來(lái)判斷 滑動(dòng)的方向
* 假設(shè) 兩點(diǎn)(x1试吁,0)棺棵,(x2,0);
* 如果 x1 -》x2 滑動(dòng)烛恤,如果向右邊滑動(dòng) 那么 x2 > x1;那么x1-x2<0;
***/
CGFloat vX = [pan velocityInView:self.view].x;
if (vX < 0) { // 說(shuō)明是向右邊滑動(dòng)
NSInteger tmpSelectedIndex = self.selectedIndex +1;
if (tmpSelectedIndex < self.viewControllers.count) {
self.selectedIndex = tmpSelectedIndex;
}
} else {
NSInteger tmpSelectedIndex = self.selectedIndex -1;
if (tmpSelectedIndex >= 0) {
self.selectedIndex = tmpSelectedIndex;
}
}
break;
}
case UIGestureRecognizerStateChanged: {
/* 比例的更新**/
[self.tabBarDelegateManager.interactiveTransition updateInteractiveTransition:progress];
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled: {
if (progress > 0.5) {
[self.tabBarDelegateManager.interactiveTransition finishInteractiveTransition];
} else {
[self.tabBarDelegateManager.interactiveTransition cancelInteractiveTransition];
}
self.tabBarDelegateManager.interactive = NO;
break;
}
default: {
break;
}
}
}
@end
里面的
動(dòng)畫(huà)MyTransitionTabBarAnimator
主要是實(shí)現(xiàn)動(dòng)畫(huà)時(shí)間
和動(dòng)畫(huà)交互
母怜,接下來(lái)就是交互 我們通過(guò)MyTransitionTabbarDelegateManager
里面的UIPercentDrivenInteractiveTransition
主要是控制百分比、取消和完成
2 . 導(dǎo)航的切換
也是一樣我們定義一個(gè)對(duì)象遵循
<UINavigationControllerDelegate>
協(xié)議的一個(gè)對(duì)象MyNaiTransitionManager
來(lái)管理 里面的動(dòng)畫(huà)
和交互
3 . present的切換
也是一樣棒动,假設(shè) vc1 =present =》vc2糙申,這個(gè)時(shí)候需要實(shí)現(xiàn) vc2的代理
UIViewControllerTransitioningDelegate
的實(shí)例MyVCTransionManager
,其實(shí)我們可以要知道 在vc2 中我們才能獲取到 vc1的view 和vc2的view