【iOS学习笔记 15-11-06】简单自定义navigationcontroller push和pop动画效果

来源:互联网 发布:603881数据港股吧 编辑:程序博客网 时间:2024/04/29 11:21

最近在做项目的时候发现presentViewController 的时候和MBProgressHUD冲突了,当时不想修改MBProgressHUD的代码,所以就另外想了一个方法,利用pushViewController来解决这个问题,但是push的动画是从左至右的,和present从下至上的动画有区别的。

于是想到重写push的动画,思路如下:


首先创建一个UINavigationController+CustomAnimation的category(这里不了解category的同学可以看看


直接上代码


//UINavigationController+CustomAnimation的category.h#import <UIKit/UIKit.h>@interface UINavigationController (CustomAnimation)- (void)pushViewController: (UIViewController*)controller;- (UIViewController*)popViewControllerAnimatedWithTransition;@end//UINavigationController+CustomAnimation的category.m#import "UINavigationController+CustomAnimation.h"@implementation UINavigationController (CustomAnimation)- (void)pushViewController: (UIViewController*)controller{        CATransition *transition = [CATransitionanimation];    transition.duration =0.3;    transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];    transition.type =kCATransitionMoveIn;    transition.subtype =kCATransitionFromTop;    self.navigationBarHidden =NO;    [selfpushViewController:controlleranimated:NO];    [self.view.layeraddAnimation:transitionforKey:nil];}- (UIViewController*)popViewControllerAnimatedWithTransition {        CATransition *transition = [CATransitionanimation];    transition.duration =0.3;    transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];    transition.type =kCATransitionReveal;    transition.subtype =kCATransitionFromBottom;    self.navigationBarHidden =NO;        [self.view.layeraddAnimation:transitionforKey:nil];        UIViewController* poppedController = [selfpopViewControllerAnimated:NO];        return poppedController;}@end



利用CATransition来实现push和pop动画

transition.type = kCATransitionMoveIn;//枚举量,表示动画类型,有3D旋转,淡入淡出,翻页和移动入场等

transition.subtype = kCATransitionFromTop;//枚举量,动画进入的方向和出去的方向,从上、下、左、右四个方向


然后在自己的项目里面import category,直接调用上面的方法就OK了,表面上是模态视图的动画效果,实际上是controller入栈操作


PS:后面要研究下引入MBProgressHUD,导致presentViewController崩溃的原因,有知道的童鞋希望不吝赐教




0 0
原创粉丝点击