服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现转场动画的3种方法示例

iOS实现转场动画的3种方法示例

2021-05-21 14:10雪山飞狐_91ae IOS

这篇文章主要给大家介绍了关于iOS实现转场动画的3种方法,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

什么是转场动画

在 navigationcontroller 里 push 或 pop 一个 view controller,在 tabbarcontroller 中切换到其他 view controller,以 modal 方式显示另外一个 view controller,这些都是 view controller transition。在 storyboard 里,每个 view controller 是一个 scene,view controller transition 便是从一个 scene 转换到另外一个 scene, 中文称呼其为「转场」。 顾名思义,转场动画便是 view controller transition 过程中的动画效果。

在 ios 7 之前,我们只能使用系统提供的转场效果,大部分时候够用,但仅仅是够用而已,总归会有各种不如意的小地方,但我们却无力改变;ios 7 开放了相关 api 允许我们对转场效果进行全面定制,这太棒了,自定义转场动画以及对交互手段的支持带来了无限可能。

本文主要给大家介绍了关于ios实现转场动画的3种方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧

1.catransition

catransition是caanimation的子类,用于过渡动画或转场动画。为视图层移入移除屏幕提供转场动画。首先来看一下简单的demo:

?
1
2
3
4
5
6
7
8
9
catransition *animation = [catransition animation];
animation.type = kcatransitionfade;
animation.subtype = kcatransitionfromright;
animation.duration = 1.0;
// 在window上执行catransition, 即可在viewcontroller转场时执行动画
[self.view.window.layer addanimation:animation forkey:@"ktransitionanimation"];
 
aviewcontroller *vc = [[aviewcontroller alloc] init];
[self presentviewcontroller:vc animated:no completion:nil];

将该动画添加到window.layer上,则会present或push时使用指定的转场动画。

其中最主要的两个属性就是type和subtype。

  • type:转场动画的类型。

官方sdk只提供了四种转场动画的类型,即:

?
1
2
3
4
ca_extern nsstring * const kcatransitionfade;
ca_extern nsstring * const kcatransitionmovein;
ca_extern nsstring * const kcatransitionpush;
ca_extern nsstring * const kcatransitionreveal;

私有的type:

?
1
2
3
4
5
6
7
8
nsstring *const kcatransitioncube = @"cube";
nsstring *const kcatransitionsuckeffect = @"suckeffect";
nsstring *const kcatransitionoglflip = @"oglflip";
nsstring *const kcatransitionrippleeffect = @"rippleeffect";
nsstring *const kcatransitionpagecurl = @"pagecurl";
nsstring *const kcatransitionpageuncurl = @"pageuncurl";
nsstring *const kcatransitioncamerairishollowopen = @"camerairishollowopen";
nsstring *const kcatransitioncamerairishollowclose = @"camerairishollowclose";
  • subtype:动画类型的方向
?
1
2
3
4
ca_extern nsstring * const kcatransitionfromright;
ca_extern nsstring * const kcatransitionfromleft;
ca_extern nsstring * const kcatransitionfromtop;
ca_extern nsstring * const kcatransitionfrombottom;

上面讲的是给window.layer添加transition,这样使得在present或push时使用指定的转场动画。

既然讲到这里了,就看一下把transition加在layer上。

看一下示例代码:

?
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
- (void)viewdidload {
 [super viewdidload];
 
 uibutton *button = [[uibutton alloc] initwithframe:cgrectmake(100, 100, 100, 50)];
 
 [button settitle:@"进入" forstate:uicontrolstatenormal];
 
 button.backgroundcolor = [uicolor redcolor];
 
 [button addtarget:self action:@selector(buttonclicked) forcontrolevents:uicontroleventtouchupinside];
 
 [self.view addsubview:button];
 
 _imageview = [[uiimageview alloc] initwithframe:cgrectmake(100, 300, 150, 150)];
 [self.view addsubview:_imageview];
 _imageview.backgroundcolor = [uicolor redcolor];
 _imagearray = @[[uiimage imagenamed:@"成果秀1"],[uiimage imagenamed:@"点赞他人1"],[uiimage imagenamed:@"偷师学艺1"],[uiimage imagenamed:@"学会欣赏3"]];
 
 _imageview.image = [uiimage imagenamed:@"成果秀1"];
}
 
 
- (void)buttonclicked{
 
 catransition *animation = [catransition animation];
 animation.type = @"cube";
 animation.subtype = kcatransitionfromright;
 animation.duration = 1.0;
 //换图片的时候使用转场动画
 [self.imageview.layer addanimation:animation forkey:nil];
 //cycle to next image
 uiimage *currentimage = self.imageview.image;
 nsuinteger index = [self.imagearray indexofobject:currentimage];
 index = (index + 1) % [self.imagearray count];
 self.imageview.image = self.imagearray[index];
 
}

2.transitionfromviewcontroller

uiviewcontroller自带的方法:
transitionfromviewcontroller:toviewcontroller:duration:options:animations:completion:这个转场动画是用在当一个父视图控制器中有几个childviewcontroller,当要在这几个子视图控制器之间切换时就可以用这个方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
aviewcontroller *a = self.childviewcontrollers[0];
bviewcontroller *b = self.childviewcontrollers[1];
cviewcontroller *c = self.childviewcontrollers[2];
 
// curl 翻页效果
// uiviewanimationoptiontransitioncurlup, uiviewanimationoptiontransitioncurldown
// flip 翻转效果
// uiviewanimationoptiontransitionflipfromleft, uiviewanimationoptiontransitionflipfromright
// uiviewanimationoptiontransitionflipfromtop, uiviewanimationoptiontransitionflipfromdown
 
[self transitionfromviewcontroller:_currentviewcontroller
   toviewcontroller:b
    duration:0.5
    options:uiviewanimationoptiontransitionflipfromright
   animations:^{
 
} completion:^(bool finished) {
 
}];

3.transition animation

1 uinavigationcontrollerdelegate + uiviewcontrolleranimatedtransitioning

在uinavigationcontroller的转场动画中,要指定uinavigationcontrollerdelegate对象:

?
1
2
self.navigationcontroller.delegate = self;
[self.navigationcontroller pushviewcontroller:itemvc animated:yes];

uinavigationcontrollerdelegate主要有以下两个协议方法:

?
1
2
3
4
5
6
7
8
//pop
- (nullable id <uiviewcontrollerinteractivetransitioning>)navigationcontroller:(uinavigationcontroller *)navigationcontroller
       interactioncontrollerforanimationcontroller:(id <uiviewcontrolleranimatedtransitioning>) animationcontroller ns_available_ios(7_0);
//push
- (nullable id <uiviewcontrolleranimatedtransitioning>)navigationcontroller:(uinavigationcontroller *)navigationcontroller
         animationcontrollerforoperation:(uinavigationcontrolleroperation)operation
            fromviewcontroller:(uiviewcontroller *)fromvc
             toviewcontroller:(uiviewcontroller *)tovc ns_available_ios(7_0);

首先创建一个基类pdanimatorbasetransition实现uiviewcontrolleranimatedtransitioning协议的方法。

uiviewcontrolleranimatedtransitioning协议的方法有:

?
1
2
3
4
5
6
//动画持续时间
- (nstimeinterval)transitionduration:(nullable id <uiviewcontrollercontexttransitioning>)transitioncontext;
//转场动画实现细节
- (void)animatetransition:(id <uiviewcontrollercontexttransitioning>)transitioncontext;
动画结束时调用
- (void)animationended:(bool) transitioncompleted;

pdanimatorbasetransition.h

?
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
#import <foundation/foundation.h>
#import <uikit/uikit.h>
 
typedef ns_enum(nsinteger, pdanimationtype){
 
 animationtypepresent = 0,
 animationtypedismiss ,
 animationtypepush ,
 animationtypepop,
};
 
@interface pdanimatorbasetransition : nsobject <uiviewcontrolleranimatedtransitioning>
 
@property (nonatomic, assign)pdanimationtype animationtype;
 
@property (nonatomic, strong)uiview *containerview;
 
@property (nonatomic, strong)uiviewcontroller *from;
 
@property (nonatomic, strong)uiviewcontroller *to;
 
@property (nonatomic, strong)uiview *fromview;
 
@property (nonatomic, strong)uiview *toview;
 
@property (nonatomic, weak)id <uiviewcontrollercontexttransitioning> transitioncontext;
 
 
@end

pdanimatorbasetransition.m

?
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
#import "pdanimatorbasetransition.h"
@interface pdanimatorbasetransition()
@end
@implementation pdanimatorbasetransition
 
#pragma mark -required
- (nstimeinterval)transitionduration:(nullable id <uiviewcontrollercontexttransitioning>)transitioncontext{
 
 return 1.f;
}
 
- (void)animatetransition:(id <uiviewcontrollercontexttransitioning>)transitioncontext{
 
 _transitioncontext = transitioncontext;
 
 _containerview = [transitioncontext containerview];
 
 _from = [transitioncontext viewcontrollerforkey:uitransitioncontextfromviewcontrollerkey];
 
 _to = [transitioncontext viewcontrollerforkey:uitransitioncontexttoviewcontrollerkey];
 
 if([transitioncontext respondstoselector:@selector(viewforkey:)]){
  
  _fromview = [transitioncontext viewforkey:uitransitioncontextfromviewkey];
  _toview = [transitioncontext viewforkey:uitransitioncontexttoviewkey];
 }else{
  
  _fromview = _from.view;
  _toview = _to.view;
 }
 
 if(self.animationtype == animationtypepresent){
  
  [self animationpresent];
 }else if (self.animationtype == animationtypedismiss){
  
  [self animationdismiss];
 }else if (self.animationtype == animationtypepop){
  
  [self animationpop];
 }else{
  
  [self animationpush];
 }
 
}
#pragma mark -optional
 
//动画结束时回调
- (void)animationended:(bool) transitioncompleted{
}
- (void)animationpresent{
}
- (void)animationdismiss{
}
- (void)animationpop{
}
- (void)animationpush{
 
}

然后创建子类pdanimatorpushpoptransition继承自pdanimatorbasetransition,实现- (void)animationpush,- (void)animationpop方法。

pdanimatorpushpoptransition.h

?
1
2
3
4
5
6
7
8
9
10
11
#import "pdanimatorbasetransition.h"
#import <uikit/uikit.h>
 
@interface pdanimatorpushpoptransition : pdanimatorbasetransition
@property (nonatomic, assign)cgpoint itemcenter;
 
@property (nonatomic, assign)cgsize itemsize;
 
@property (nonatomic, strong)nsstring *imagename;
 
@end

pdanimatorpushpoptransition.m

?
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
#import "pdanimatorpushpoptransition.h"
 
@implementation pdanimatorpushpoptransition
 
- (instancetype)init{
 
 self = [super init];
 if(self){
  
  
 }
 
 return self;
}
 
- (nstimeinterval)transitionduration:(nullable id <uiviewcontrollercontexttransitioning>)transitioncontext{
 
 return 5.f;
}
 
- (void)animatetransition:(id <uiviewcontrollercontexttransitioning>)transitioncontext{
 
 [super animatetransition:transitioncontext];
}
 
- (void)animationpush{
 
 nstimeinterval duration = [self transitionduration:self.transitioncontext];
 __weak typeof(self) weakself = self;
 
 self.containerview.backgroundcolor = [uicolor lightgraycolor];
 
 uiimageview *imageview = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, 100, 160)];
 imageview.image = [uiimage imagenamed:self.imagename];
 imageview.center = _itemcenter;
 
 cgfloat initialscale = _itemsize.width / cgrectgetwidth(imageview.frame);
 
 cgaffinetransform transform = cgaffinetransformidentity;
 
 transform = cgaffinetransformscale(transform, initialscale, initialscale);
 transform = cgaffinetransformrotate(transform, m_pi);
 
 imageview.layer.affinetransform = transform;
 
 // imageview.transform = cgaffinetransformmakescale(initialscale, initialscale);
 
 [self.containerview addsubview:imageview];
 
 
 self.toview.frame = [self.transitioncontext finalframeforviewcontroller:self.to];
 cgpoint finalcenter = self.toview.center;
 self.toview.center = finalcenter;
 self.toview.alpha = 0.0;
 //这一句一定要
 [self.containerview addsubview:self.toview];
 
 [uiview animatewithduration:duration delay:0 usingspringwithdamping:0.5 initialspringvelocity:0 options:uiviewanimationoptioncurvelinear animations:^{
  
 
  imageview.layer.affinetransform = cgaffinetransformidentity;
  
  imageview.center = finalcenter;
  
  self.fromview.alpha = 0.0;
  self.containerview.backgroundcolor = [uicolor redcolor];
 } completion:^(bool finished){
  
  [imageview removefromsuperview];
  
  weakself.toview.alpha = 1.0f;
  weakself.fromview.alpha = 1.0f;
  
  [weakself.transitioncontext completetransition:![weakself.transitioncontext transitionwascancelled]];
 }];
 
}
 
- (void)animationpop{
 
 nstimeinterval duration = [self transitionduration:self.transitioncontext];
 __weak typeof(self) weakself = self;
 
 self.toview.frame = [self.transitioncontext finalframeforviewcontroller:self.to];
 
 [self.containerview insertsubview:self.toview belowsubview:self.fromview];
 
 self.fromview.alpha = 0.0;
 
 self.fromview.backgroundcolor = [uicolor clearcolor];
 
 [uiview animatewithduration:duration delay:0 usingspringwithdamping:0.5 initialspringvelocity:0 options:uiviewanimationoptioncurvelinear animations:^{
  
  cgfloat initialscale = _itemsize.width / 200;
  weakself.fromview.transform = cgaffinetransformmakescale(initialscale, initialscale);
  weakself.fromview.center = weakself.itemcenter;
  
  weakself.toview.alpha = 1.0f;
 } completion:^(bool finished){
  
  weakself.fromview.alpha = 0.0f;
  
  [weakself.transitioncontext completetransition:![weakself.transitioncontext transitionwascancelled]];
 }];
}
 
 
@end

然后我们在需要push的地方实现uinavigationcontrollerdelegate的协议方法:

?
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
- (nullable id <uiviewcontrolleranimatedtransitioning>)navigationcontroller:(uinavigationcontroller *)navigationcontroller
           animationcontrollerforoperation:(uinavigationcontrolleroperation)operation
               fromviewcontroller:(uiviewcontroller *)fromvc
               toviewcontroller:(uiviewcontroller *)tovc ns_available_ios(7_0){
 
 pdanimatorpushpoptransition *animationtransition = [[pdanimatorpushpoptransition alloc] init];
 if(operation == uinavigationcontrolleroperationpush){
  
  animationtransition.animationtype = animationtypepush;
 }else if (operation == uinavigationcontrolleroperationpop){
  
  animationtransition.animationtype = animationtypepop;
 }
 
 nsarray *indexpaths = [self.collectionview indexpathsforselecteditems];
 if (indexpaths.count == 0) {
  return nil;
 }
 
 nsindexpath *selectedindexpath = indexpaths[0];
 uicollectionviewcell *cell = [self.collectionview cellforitematindexpath:selectedindexpath];
 
 // 一定要加上convertpoint:toview:操作
 animationtransition.itemcenter = [self.collectionview convertpoint:cell.center toview:self.view];
 animationtransition.itemsize = cell.frame.size;
 animationtransition.imagename = [nsstring stringwithformat:@"%ld", (long)selectedindexpath.item];
 
 return animationtransition;
}

2 uiviewcontrollertransitioningdelegate+uiviewcontrolleranimatedtransitioning

首先需要设置被present的controller的transitiondelegate

?
1
2
3
demoviewcontrollertransitionpresentedviewcontroller *presentedvc = [[demoviewcontrollertransitionpresentedviewcontroller alloc] init];
presentedvc.transitiondelegate = self;
[self presentviewcontroller:presentedvc animated:yes completion:nil];

uiviewcontrollertransitioningdelegate的代理的协议方法有:

?
1
2
3
4
5
6
7
8
//  prenent 
- (id )animationcontrollerforpresentedcontroller:(uiviewcontroller *)presented presentingcontroller:(uiviewcontroller *)presenting sourcecontroller:(uiviewcontroller *)source;
//  pop 
- (id )animationcontrollerfordismissedcontroller:(uiviewcontroller *)dismissed;
//  prenent 
- (id )interactioncontrollerforpresentation:(id )animator;
//  pop 
- (nullable id )interactioncontrollerfordismissal:(id )animator;

创建子类pdanimationpresenttransitio继承自基类pdanimatorbasetransition,实现- (void)animationpresent,- (void)animationdismiss方法。

pdanimationpresenttransition.h

?
1
2
3
4
5
#import "pdanimatorbasetransition.h"
 
@interface pdanimationpresenttransition : pdanimatorbasetransition
 
@end

pdanimationpresenttransition.m

?
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
#import "pdanimationpresenttransition.h"
 
@implementation pdanimationpresenttransition
 
- (nstimeinterval)transitionduration:(nullable id <uiviewcontrollercontexttransitioning>)transitioncontext{
 
 return 1.f;
}
 
- (void)animatetransition:(id <uiviewcontrollercontexttransitioning>)transitioncontext{
 [super animatetransition:transitioncontext];
}
 
- (void)animationpresent{
 
 nstimeinterval duration = [self transitionduration:self.transitioncontext];
 __weak typeof(self) weakself = self;
 
 self.toview.frame = [self.transitioncontext initialframeforviewcontroller:self.to];
 
 self.fromview.frame = [self.transitioncontext initialframeforviewcontroller:self.from];
 
 cgaffinetransform transform = cgaffinetransformidentity;
 transform = cgaffinetransformscale(transform, 0.001, 0.001);
 self.toview.layer.affinetransform = transform;
 
 [self.containerview addsubview:self.toview];
 
 self.toview.alpha = 0.0;
 
 [uiview animatewithduration:duration delay:0 usingspringwithdamping:0.6 initialspringvelocity:0 options:uiviewanimationoptioncurveeaseinout animations:^{
  
  self.toview.layer.affinetransform = cgaffinetransformidentity;
  self.toview.frame = [self.transitioncontext finalframeforviewcontroller:self.to];
  self.toview.alpha = 1.0;
 } completion:^(bool finished){
  
  bool wascancelled = [weakself.transitioncontext transitionwascancelled];
  [weakself.transitioncontext completetransition:!wascancelled];
 }];
 
}

然后我们在需要present的地方实现uiviewcontrollertransitioningdelegate的代理方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
- (nullable id <uiviewcontrolleranimatedtransitioning>)animationcontrollerforpresentedcontroller:(uiviewcontroller *)presented presentingcontroller:(uiviewcontroller *)presenting sourcecontroller:(uiviewcontroller *)source{
 
 pdanimationpresenttransition *animationtransition = [[pdanimationpresenttransition alloc] init];
 animationtransition.animationtype = animationtypepresent;
 return animationtransition;
}
 
- (nullable id <uiviewcontrolleranimatedtransitioning>)animationcontrollerfordismissedcontroller:(uiviewcontroller *)dismissed{
 
 pdanimationpresenttransition *animationtransition = [[pdanimationpresenttransition alloc] init];
 animationtransition.animationtype = animationtypepresent;
 return animationtransition;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/da5376a6ab18

延伸 · 阅读

精彩推荐
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03