定制controller转场动画
定制controller转场动画

从iOS7开始就可以自由定制控制器间的转场动画了,以下实例描述最简单的定制方式,达到的效果如下所示:

为了实现这个效果需要这么多的文件-_-!!!!

RootViewController
//
// RootViewController.h
// ControllerCustom
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
RootViewController.h
//
// RootViewController.m
// ControllerCustom
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h" #import "PresentingAnimator.h"
#import "DismissingAnimator.h" #import "ModelViewController.h" @interface RootViewController ()<UIViewControllerTransitioningDelegate> @property (nonatomic, strong) UIButton *button; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor]; _button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
_button.backgroundColor = [UIColor blackColor];
_button.layer.cornerRadius = ;
[_button setTitle:@"present"
forState:UIControlStateNormal];
_button.center = self.view.center;
[self.view addSubview:_button]; [_button addTarget:self
action:@selector(buttonEvent:)
forControlEvents:UIControlEventTouchUpInside];
} - (void)buttonEvent:(id)sender
{
// 推出控制器
ModelViewController *modalViewController = [ModelViewController new]; // 设置转场动画代理
modalViewController.transitioningDelegate = self; // 定制转场动画
modalViewController.modalPresentationStyle = UIModalPresentationCustom; [self presentViewController:modalViewController
animated:YES
completion:NULL];
} - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
// 推出控制器的动画
return [PresentingAnimator new];
} - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
// 退出控制器动画
return [DismissingAnimator new];
} @end
RootViewController.m
ModelViewController
//
// ModelViewController.h
// ControllerCustom
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface ModelViewController : UIViewController @end
ModelViewController.h
//
// ModelViewController.m
// ControllerCustom
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "ModelViewController.h" @interface ModelViewController () @property (nonatomic, strong) UIButton *button; @end @implementation ModelViewController - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor]; _button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
_button.backgroundColor = [UIColor blackColor];
_button.layer.cornerRadius = ;
[_button setTitle:@"dismiss"
forState:UIControlStateNormal];
_button.center = self.view.center;
[self.view addSubview:_button]; [_button addTarget:self
action:@selector(buttonEvent:)
forControlEvents:UIControlEventTouchUpInside];
} - (void)buttonEvent:(id)sender
{
[self dismissViewControllerAnimated:YES
completion:^{ }];
} @end
ModelViewController.m
PresentingAnimator
//
// PresentingAnimator.h
// ControllerCustom
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h> @interface PresentingAnimator : NSObject<UIViewControllerAnimatedTransitioning> @end
PresentingAnimator.h
//
// PresentingAnimator.m
// ControllerCustom
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "PresentingAnimator.h" @implementation PresentingAnimator // 转场动画时间
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.5f;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// 自己的view
UIView *fromView = \
[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view; // 另一个view
UIView *toView = \
[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view; // 管理容器
UIView *container = [transitionContext containerView];
container.backgroundColor = [UIColor blackColor]; // 容器中添加推出的view
[container addSubview:fromView];
[container addSubview:toView]; // 开始动画(移出fromView,移进toView)
[UIView animateWithDuration:[self transitionDuration:transitionContext]
animations:^{
fromView.frame = CGRectMake(, , -, -); // 设置toView从右侧偏移进来
CGRect toFrame = toView.frame;
toFrame.origin.x = container.bounds.size.width; // 偏移一个控制器
toView.frame = toFrame;
toView.center = container.center; } completion:^(BOOL finished) {
// 动画结束
[transitionContext completeTransition:YES];
}];
} @end
PresentingAnimator.m
DismissingAnimator
//
// DismissingAnimator.h
// Popping
//
// Created by André Schneider on 16.05.14.
// Copyright (c) 2014 André Schneider. All rights reserved.
// #import <Foundation/Foundation.h> @interface DismissingAnimator : NSObject <UIViewControllerAnimatedTransitioning> @end
DismissingAnimator.h
//
// DismissingAnimator.m
// Popping
//
// Created by André Schneider on 16.05.14.
// Copyright (c) 2014 André Schneider. All rights reserved.
// #import "DismissingAnimator.h" @implementation DismissingAnimator - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.5f;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// 自己的view
UIView *fromView = \
[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view; // 另一个view
UIView *toView = \
[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
toView.frame = CGRectMake(, , -, -); // 管理容器
UIView *container = [transitionContext containerView]; // 容器中添加推出的view
[container addSubview:toView];
[container addSubview:fromView]; container.backgroundColor = [UIColor blackColor]; // 开始动画(移出fromView,移进toView)
[UIView animateWithDuration:[self transitionDuration:transitionContext]
animations:^{
CGRect fromFrame = fromView.frame;
fromFrame.origin.x = container.bounds.size.width;
fromView.frame = fromFrame; toView.frame = container.frame; } completion:^(BOOL finished) {
// 动画结束
[transitionContext completeTransition:YES];
}];
} @end
DismissingAnimator.m
核心的地方:

为什么设计成代理呢?其实,这是为了让基本的控制器(推出其他控制器的控制器)持有被推出的控制器而已,我是这么理解的.
为了能够实现控制器间的转场动画,我们需要一个实现了UIViewControllerAnimatedTransitioning协议的对象才行.
也就是PresentingAnimator以及DismissingAnimator

最少实现里面的两个方法:
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext

完了,就是这么简单呢.
附录:

这个方法非常关键哦,动画执行完了之后记得设置好了.

fromView本身就被transitionContext包含拥有了,你无须进行上面的那个addSubview操作哦,可以直接去掉即可

定制controller转场动画的更多相关文章
- iOS 动画学习之视图控制器转场动画
一.概述 1.系统会创建一个转场相关的上下文对象,传递到动画执行器的animateTransition:和transitionDuration:方法,同样,也会传递到交互Controller的star ...
- iOS - 转场动画
苹果在 iOS7 定制了 ViewController 的切换效果 一 在iOS5和iOS6之前,ViewController的切换主要有4种 Push/Pop,NavigationViewCotnr ...
- iOS 开发--转场动画
"用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下:" 本文主讲SWIFT版,OC版在后面会留下Demo下载 在iOS中,在同 ...
- 第六十五篇、OC_iOS7 自定义转场动画push pop
自定义转场动画,在iOS7及以上的版本才开始出现的,在一些应用中,我们常常需要定制自定义的的跳转动画 1.遵守协议:<UIViewControllerAnimatedTransitioning& ...
- 转场动画2-Pop动画
上一篇试讲push动画,这篇分解pop动画 里面关于矩阵有不懂得,参考CATransform3D 特效详解 上图(虚拟机下,图是渣渣 ) 代码直接上 // // PopTransition.h // ...
- iOS自定义转场动画的实现
iOS中熟悉的是导航栏中的push和pop这两种动画效果,在这里我们可以自己实现自己想要的一些转场动画 下面是我自己创建转场动画的过程 1.新建一个文件继承自NSObject ,遵循协议UIViewC ...
- Storyboard 自定义转场动画
在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,segue的类型是不同的.在iphone中,segue 有:push,modal,和custom三种不同的类型, ...
- iOS 转场动画探究(二)
这篇文章是接着第一篇写的,要是有同行刚看到的话建议从前面第一篇看,这是第一篇的地址:iOS 转场动画探究(一) 接着上一篇写的内容: 上一篇iOS 转场动画探究(一)我们说到了转场要素的第四点,把那个 ...
- iOS转场动画封装
写在前面 iOS在modal 或push等操作时有默认的转场动画,但有时候我们又需要特定的转场动画效果,从iOS7开始,苹果就提供了自定义转场的API,模态推送present和dismiss.导航控制 ...
随机推荐
- tomcat启动(一)startup.bat|catalina.bat分析
环境:windows X64位 Tomcat8.0.47 bootstrap.jar是tomcat的内核 开始位置 startup.bat 查看文本 具体的批处理脚本语法可以查看我整理的文章 http ...
- 几句话说说跨IDC分布式数据库Calvin
CalvinFS拿了FAST 15最佳论文:找到了失联十三年的小伙伴:年终/年初整理资料,发现做团委工作的 King 师兄对Calvin有兴趣:最近其他团队对分布式事务和存储问题/兴趣较多……几件事激 ...
- 异常空格,ASCII (194,160)问题
今天运营的同学反映有一些店铺的名称后面带空格,我下意识的说不可能啊,我已经处理过了啊.然后就找出来看. 其中有个店铺的名称是“安踏 ”,第一眼看上去好像是带了个空格.然后我就仔细的看了下. pry(m ...
- Excel文件上传存储到数据库
- 【转】marquee标签简介
本文转自:http://www.360doc.com/content/12/0818/16/8351655_230872993.shtml marquee语法 <marquee>&l ...
- Django控制器
配置路由 通过对urls.py的配置将用户请求映射到处理函数. Django的URL字符串匹配实际上基于正则表达式,这允许单条URL可以匹配一类请求.参见Django Book中的示例: from d ...
- [CQOI 2018]解锁屏幕
Description 题库链接 给出平面上 \(n\) 个点,一开始你可以选任何一个点作为起点,接着对于每一个你在的位置,你可以选取一个未走过的点.将路径(线段)上所有的点均选上(包括起点终点),并 ...
- 从数据库表中随机获取N条记录的SQL语句
Oracle: select * from (select * from tableName order by dbms_random.value) where rownum < N MS SQ ...
- 设计模式学习——工厂模式(Factory Pattern)
1.有一个工厂,专门生产不同品牌的汽车.当有人需要从此工厂提货的时候,只需要告诉他,要什么品牌的,就可以了,并不关心这些车是怎么生产出来的. 2.以上方式,如果增加品牌的时候,也要修改工厂,有点麻烦. ...
- javascript数组元素全排列
多个数组(数量不定)例如三个数组 {a,b} {1,2} {d}排列组合后为a,1,da,2,db,1,db,2,d是js的算法哦 var arr = [["a","b& ...