iOS 7以后提供了自定义转场动画的功能,我们可以通过遵守协议完成自定义转场动画。本篇文章讲解如何实现自定义presentdismiss自定义动画。

效果图

本篇文章实现的动画切换效果图如下:

iOS 7 present/dismiss转场动画

视图切换种类

如下效果图,这是有两大类视图切换动画的,一种是交互式的,另一种就是自定义的。

iOS 7 present/dismiss转场动画

本篇只讲其中的UIViewControllerAnimatedTransitioning协议,来实现presentdismiss动画效果。另外的几个,后面会继续学习总结!!!

协议

我们要实现presentdismiss自定义转场效果,我们必须要有一个遵守了UIViewControllerAnimatedTransitioning协议且实现其必须实现的代理方法的类。

我们先来学习UIViewControllerAnimatedTransitioning协议:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
<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.
//
// 指定转场动画时长,必须实现,否则会Crash。
// 这个方法是为百分比驱动的交互转场和有对比动画效果的容器类控制器而定制的。
;
 
// This method can only  be a nop if the transition is interactive
// and not a percentDriven interactive transition.
// 若非百分比驱动的交互过渡效果,这个方法只能为空
;
 
 
optional
 
// This is a convenience and if implemented will be invoked by the system
// when the transition context's completeTransition: method is invoked.
;
 
@end
 

我们要实现目标效果,就需要一个定义一个类遵守UIViewControllerAnimatedTransitioning协议并实现相应的代理方法。

遵守UIViewControllerAnimatedTransitioning协议

下面,我们来定义一个转场类,这个类必须要遵守UIViewControllerAnimatedTransitioning协议,如下:

头文件

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
//
//  HYBModalTransition.h
//  PresentDismissTransitionDemo
//
//  Created by huangyibiao on 15/12/21.
//  Copyright © 2015年 huangyibiao. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
 
{
,
2
;
 
<UIViewControllerAnimatedTransitioning>
 
/*!
*  @author 黄仪标, 15-12-21 11:12:44
*
*  指定动画类型
*
*  @param type          动画类型
*  @param duration      动画时长
*  @param presentHeight 弹出呈现的高度
*  @param scale         fromVC的绽放系数
*
*  @return
*/
type
duration
presentHeight
;
 
@end
 

我们只公开了一个方法来创建,指定动画类型,动画时长,呈现的高度,缩放系数。

实现文件

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 
//
//  HYBModalTransition.m
//  PresentDismissTransitionDemo
//
//  Created by huangyibiao on 15/12/21.
//  Copyright © 2015年 huangyibiao. All rights reserved.
//
 
 
)
 
;
;
;
;
 
@end
 
HYBModalTransition
 
type
duration
presentHeight
{
;
  
;
;
;
;
  
;
}
 
#pragma mark - UIViewControllerAnimatedTransitioning
{
;
}
 
{
;
}
 
{
{
{
;
;
}
{
;
;
}
{
;
}
}
}
 
#pragma mark - Private
{
;
;
;
  
// 对fromVC.view的截图添加动画效果
;
;
  
// 对截图添加动画,则fromVC可以隐藏
;
  
// 要实现转场,必须加入到containerView中
;
;
  
// 我们要设置外部所传参数
// 设置呈现的高度
,
,
,
;
  
// 开始动画
;
{
// 在Y方向移动指定的高度
;
    
// 让截图缩放
;
{
{
;
}
;
}
 
{
;
;
;
  
// 取出present时的截图用于动画
;
  
// 开始动画
{
;
;
 
{
{
;
;
      
// 将截图去掉
;
}
;
}
 
@end
 

我们这里就不细讲了,因为在iOS 7 push/pop转场动画中已经讲过了。大家若未看过,可以先阅读。

测试效果

我们要设置一下被present的控制器的代理,在-viewDidLoad:时添加如下代码:

 
1
2
3
4
5
 
// 配置一下代理防呈现样式为自定义
;
;
 

同时,还需要遵守协议并实现协议UIViewControllerTransitioningDelegate,这个是控制器转场动画实现的代理:

 
1
2
3
4
5
6
7
8
9
10
 
#pragma mark - UIViewControllerTransitioningDelegate
{
;
}
 
{
;
}
 

我们设置presentdismiss自定义对象,就可以实现我们的动画了。

想要实现什么样的动画,都可以在HYBModalTransition类里面实现,没有实现不了,只有想不到!!!

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-22
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-30
  • 2021-07-06
相关资源
相似解决方案