iOS自定义转场动画的实现
iOS中熟悉的是导航栏中的push和pop这两种动画效果,在这里我们可以自己实现自己想要的一些转场动画
下面是我自己创建转场动画的过程
1、新建一个文件继承自NSObject ,遵循协议UIViewControllerAnimatedTransitioning,
2、添加一个枚举,用于判断视图的推入还是推出
typedef enum {
AnimationTypePresent,
AnimationTypeDismiss
} AnimationType;并申明一个属性 @property (nonatomic, assign) AnimationType type;
3、在.m中分别实现下面的两个协议方法
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;- (void)animationEnded:(BOOL)transitionCompleted;-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 0.40;
}
#pragma mark -执行具体的动画
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
//拿到相关的视图层次
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViweController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.type == AnimationTypePresent) {
toViweController.view.transform = CGAffineTransformMakeScale(0.0, 0.0);
[containerView insertSubview:toViweController.view aboveSubview:fromViewController.view];
//添加动画
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViweController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}else{
//将to视图插入到from视图的底部
[containerView insertSubview:toViweController.view belowSubview:fromViewController.view];
//放大from视图直到他消失
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.transform = CGAffineTransformMakeScale(0.0, 0.0);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
#pragma mark -动画执行完毕
- (void)animationEnded:(BOOL)transitionCompleted;
4、实现了这些方法之后在你想要跳转的地方引入这个NSObject类的头文件,定义一个全局变量并初始化
self.navigationController.delegate = self;实现导航栏的代理以及协议
5、在这两个方法中都要返回自己定义个那个全局变量
#pragma mark - 跳转开始的动画协议push过去的动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
// return _animationFade;
return _animationScale;
}
#pragma mark -pop回来的动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
return _animationScale;
}
6、判断视图是推入还是推出
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
switch (operation) {
//不同类型对应不同的动画效果
case UINavigationControllerOperationPush:
_animationScale.type = AnimationTypePresent;
return _animationScale;
break;
case UINavigationControllerOperationPop:
_animationScale.type = AnimationTypeDismiss;
return _animationScale;
break;
}
return nil;
}
自定义转场动画学习站:
objc系列译文(12.3):自定义View Controller容器转场
iOS自定义转场动画的实现的更多相关文章
- iOS 自定义转场动画
代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...
- iOS 自定义转场动画浅谈
代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...
- iOS自定义转场动画实战讲解
iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...
- 一行代码实现自定义转场动画--iOS自定义转场动画集
WXSTransition 这款非常不错,力推 这是作者源码简书地址: http://www.jianshu.com/p/fd3154946919 这是作者源码github地址 https://git ...
- VCTransitionsLibrary –自定义iOS交互式转场动画的库
简介 VCTransitionsLibrary 提供了许多适用于入栈,出栈,模态等场景下控制器切换时的转场动画.它本身提供了一个定义好的转场动画库,你可以拖到自己工程中直接使用;也提供了许多拥有不同转 ...
- iOS开发自定义转场动画
1.转场动画 iOS7之后开发者可以自定义界面切换的转场动画,就是在模态弹出(present.dismiss),Navigation的(push.pop),TabBar的系统切换效果之外自定义切换动画 ...
- iOS 开发--转场动画
"用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下:" 本文主讲SWIFT版,OC版在后面会留下Demo下载 在iOS中,在同 ...
- Swift开发小技巧--自定义转场动画
自定义转场动画 个人理解为重写了被弹出控制器的modal样式,根据自己的样式来显示modal出来的控制器 例:presentViewController(aVC, animated: true, co ...
- 第六十五篇、OC_iOS7 自定义转场动画push pop
自定义转场动画,在iOS7及以上的版本才开始出现的,在一些应用中,我们常常需要定制自定义的的跳转动画 1.遵守协议:<UIViewControllerAnimatedTransitioning& ...
随机推荐
- 著名清理软件(CCleaner) 5.24.5841 中文版
软件名称: 著名清理软件(CCleaner) 软件语言: 多国语言 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 5.7MB 图片预览: 软件简介: CCleaner的体积小, ...
- 转:iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解
1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS开发者基 ...
- linux upstart启动配置
程序名.conf放在/etcc/init/目录下# 注释 description "your-server" author "xxx" start on run ...
- jvm原理及调优
一.java内存管理及垃圾回收 jvm内存组成结构 jvm栈由堆.栈.本地方法栈.方法区等部分组成,结构图如下所示: (1)堆 所有通过new创建的对象的内存都在堆中分配,堆的大小可以通过-Xmx和- ...
- ansible 判断和循环
标准循环 模式一 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - te ...
- 如何在 Linux 中找出最近或今天被修改的文件
1. 使用 ls 命令,只列出你的 home 文件夹中今天的文件. ls -al --time-style=+%D | grep `date +%D` 其中: -a- 列出所有文件,包括隐藏文件 -l ...
- tfs 清除缓存,在需要时
C:\Users\xxx\AppData\Local\Microsoft\Team Foundation\5.0
- 七天学会ASP.NET MVC (四)——Layout页面使用和用户角色管理 (代码下载)
中文翻译链接: http://www.cnblogs.com/powertoolsteam/p/MVC_four.html 360云盘: https://yunpan.cn/cYuEeLtXUvrgC ...
- Oracle使用虚拟表dual一次插入多条记录
从一个CSV文件中读取所有的数据,并且插入到一个Oracle数据库中,并且几分钟内完成,大约有60万条.网上有人说了,你可以循环insert然后插入几千条以后Commit一次,我靠,你自己试试看!!如 ...
- gridview XML
GridView动态添加模板列 http://blog.csdn.net/wljhk2006/article/details/24723219 XML与DataTable互转类 http://bl ...