iOS-授之以鱼不如授之以渔=_=带你升入理解自定义转场动画
来源:互联网 发布:有哪些数据库 编辑:程序博客网 时间:2024/04/30 00:11
1.前言
说到APP的转场动画,大家一定会想到push和pop来实现转场动画,这是苹果用了很多年提供的自带的方法,而且我相信大家在APP开发的时候,很多小伙伴都会选择自带的转场动画,很少去自定义,不论你是大神还是小白菜,本小白菜也是一样,毕竟作为程序员的我们已经很累了,项目经理不提我们也很少回去做,但是我们工作不是为了工作而工作,是为了丰富充实自己。
2.自定义UINavigationController
当UINavigationController对UIViewController进行pop或者push的时候,我们需要让UIViewController知道我们进行这些操作,这里我们需要UINavigationController的代理UINavigationControllerDelegate,UINavigationControllerDelegate的代理方法有六种,这里我们只需要一张,那就是:
#pragma mark - UINavigationControllerDelegate- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC;
代码如下:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self uiConfig];}- (void)uiConfig{ self.delegate = self;}#pragma mark - UINavigationControllerDelegate- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{ ZFJ_Animation *ZFJAnimation = [[ZFJ_Animation alloc]init]; ZFJAnimation.operation = operation; ZFJAnimation.animationType = _animationType; return (id)ZFJAnimation;}
3.自定义动画(ZFJ_Animation)
ZFJ_Animation这个类继承于NSObject,参数如下:
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>typedef NS_ENUM(NSInteger, ZFJ_AnimationType) { ZFJ_AnimationType1, ZFJ_AnimationType2, ZFJ_AnimationType3};@interface ZFJ_Animation : NSObject@property (nonatomic,assign) UINavigationControllerOperation operation;@property (nonatomic,assign) ZFJ_AnimationType animationType;//动画类型@end
参数说明
operation用来接收控制器的转场类型,UINavigationControllerOperation是个枚举类型,类型如下:
typedef NS_ENUM(NSInteger, UINavigationControllerOperation) { UINavigationControllerOperationNone, UINavigationControllerOperationPush, UINavigationControllerOperationPop,};
看到这个枚举大家就明白了,看你的UIViewController是push还是pop,这样我们可以根据这个类型来定义不同的类型;
animationType我自定义转场的动画类型,也是一个枚举,因为在ZFJ_Animation里面我打算定义几个不同的动画类型;
当然最重要的就是这个动画控制器的类必须遵循一个代理方法UIViewControllerAnimatedTransitioning,代理的里面的两个方法也必须要实现,代理方法如下:
@protocol UIViewControllerAnimatedTransitioning <NSObject>// 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;@optional代理说明
a.transitionDuration:这个方法返回自定义动画的执行时间;
b.animateTransition:这个方法是我们这个动画管理类的核心,我们需要自定义什么样的动画全部在这里面执行;
上代码:
#pragma mark - UIViewControllerAnimatedTransitioning- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{ return 0.4;}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{ UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *containerView = transitionContext.containerView; if(self.animationType == ZFJ_AnimationType1){ //动画一 [containerView addSubview:toViewController.view]; CGRect fromViewStartFrame = [transitionContext initialFrameForViewController:fromViewController]; CGRect toViewEndFrame = [transitionContext finalFrameForViewController:toViewController]; CGRect fromViewEndFrame = fromViewStartFrame; CGRect toViewStartFrame = toViewEndFrame; if(self.operation == UINavigationControllerOperationPush){ //PUSH toViewStartFrame.origin.y -= toViewEndFrame.size.height; }else if (self.operation == UINavigationControllerOperationPop){ //POP fromViewEndFrame.origin.y -= fromViewStartFrame.size.height; [containerView sendSubviewToBack:toViewController.view]; } //这里面 你可以自定义动画 fromViewController.view.frame = fromViewStartFrame; toViewController.view.frame = toViewStartFrame; //动画 CGFloat duration = [self transitionDuration:transitionContext]; [UIView animateWithDuration:duration animations:^{ fromViewController.view.frame = fromViewEndFrame; toViewController.view.frame = toViewEndFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; }else if(self.animationType == ZFJ_AnimationType2){ //动画二 }else if (self.animationType == ZFJ_AnimationType3){ //动画三 } }
说明:这里面我定义了三种动画,因为页面展示太长,我就展示一种动画。
4.UIViewController调用
- (void)nextBtnClick:(UIButton *)nextBtn{ NSInteger index = nextBtn.tag - 100; ZFJ_NavViewController *nvc = (ZFJ_NavViewController *)self.navigationController; nvc.animationType = (ZFJ_AnimationType)index; ZFJ_NextViewController *zvc = [[ZFJ_NextViewController alloc]init]; [nvc pushViewController:zvc animated:YES];}
5.效果展示
6.DEMO下载
点击下载
http://download.csdn.net/detail/u014220518/9883291
- iOS-授之以鱼不如授之以渔=_=带你升入理解自定义转场动画
- 授之以鱼,不如授之以渔
- 授之以鱼不如授之以渔
- 授之以鱼不如授之以渔
- iOS之转场动画/自定义转场动画
- 授之以鱼不如授之以渔:HijackThis详解
- 授之以鱼不如授之以渔,hosts,hosts,hosts
- ecshop 二次开发基本思想 授之以鱼,不如授人以渔
- iOS 关于自定义转场动画,以UITabBarController为例
- springmvc 请求400 bad request(授之以鱼不如授之以渔,使用firebug找错)
- ios开发之自定义转场动画
- 授您以鱼,不如授您以渔
- 授人以鱼不如授人以渔
- 授人以鱼不如授人以渔
- 授人以鱼,不如授人以渔
- 授人以鱼,不如授人以渔
- 现代化的中国教育,缺失的不是钱,是思想(之一)--“授之以鱼不如授之以渔”的局限性
- IOS图像5之UIView动画、自定义转场动画、delegate
- 软件测试术语
- QTcpsocket
- 376. Wiggle Subsequence
- 自学SEO技术?零基础学SEO没你想的那么简单
- Skynet服务器框架(八) 任务和消息调度机制
- iOS-授之以鱼不如授之以渔=_=带你升入理解自定义转场动画
- 习题
- arm汇编指令笔记
- bzoj 1047: [HAOI2007]理想的正方形
- PHP对中文字符串的截取
- hash冲突及处理算法
- Linux-tar压缩解压命令
- 款开源的图形化Redis客户端管理软件推荐
- 根据地址返回地图经纬度-JAVA