1.先看一下效果
2.在iOS7.0以前,要实现这样的效果,只有自定义TabBar了,但这很麻烦。而在iOS7.0以后,苹果在UITabBarControllerDelegate中增加了下面两个代理方法/**
* 实现该代理,即可以实现自定义的各界面切换时的动画(如平推,缩放,淡入淡出等)
* fromVC:当前显示的VC
* toVC:将要切换到的VC
* 返回一个自定义的切换动画,在本例中,我自定义了一个平推效果的动画
*/
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
animationControllerForTransitionFromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC;
/**
* 实现该代理,即可以实现与动画的交互
* tabBarController:当前的tabBarController
* animationController:动画百分比控制器
* 返回一个自定义的动画百分比控制器,以控制当前动画进行的百分比。
*/
- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController;
3.下面是实现过程
- 首先需要一个TabBarController,在本例中,我写了两个MainViewController(点击item切换时,也用自定义动画)、MainTabBarViewController(点击item是没有动画),可以在AppDelegate中选则使用哪一个。
- 在TabBarController中,添加一个pan手势
- (void)viewDidLoad { |
- pan手势的实现
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)pan{ |
- 代理的实现
- (id<UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController |
- 自定义平推的动画,自定义动画需要遵守
协议,需要实现下面方法
// 返回动画执行时间 |
- 自定义动画百分比控制器,这个苹果一个给我们定义好了一个类,我们只需要继承UIPercentDrivenInteractiveTransition,就可以轻松实现,该类主要有以下方法
// 动画时间 |
4.最后附上Demo的下载地址:https://github.com/cdcyd/CommonControlsCollection