配色: 字号:
iOS开发中常用的各种动画、页面切面效果
2016-12-09 | 阅:  转:  |  分享 
  
iOS开发中常用的各种动画、页面切面效果

这篇文章主要介绍了iOS开发中常用的各种动画、页面切面效果的相关资料,需要的朋友可以参考下

今天主要用到的动画类是CALayer下的CATransition至于各种动画类中如何继承的在这也不做赘述,网上的资料是一抓一大把。好废话少说切入今天的正题。

一.封装动画方法

1.用CATransition实现动画的封装方法如下,每句代码是何意思,请看注释之。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17 #pragmaCATransition动画实现

-(void)transitionWithType:(NSString)typeWithSubtype:(NSString)subtypeForView:(UIView)view

{

//创建CATransition对象

CATransitionanimation=[CATransitionanimation];

//设置运动时间

animation.duration=DURATION;

//设置运动type

animation.type=type;

if(subtype!=nil){

//设置子类

animation.subtype=subtype;

}

//设置运动速度

animation.timingFunction=UIViewAnimationOptionCurveEaseInOut;

[view.layeraddAnimation:animationforKey:@"animation"];

} 代码说明:

CATransition常用的属性如下:

duration:设置动画时间

type:稍后下面会详细的介绍运动类型

subtype:和type匹配使用,指定运动的方向,下面也会详细介绍

timingFunction:动画的运动轨迹,用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是

均匀变化(相同时间变化量相同)还是先快后慢,先慢后快还是先慢再快再慢.

动画的开始与结束的快慢,有五个预置分别为(下同):

kCAMediaTimingFunctionLinear线性,即匀速

kCAMediaTimingFunctionEaseIn先慢后快

kCAMediaTimingFunctionEaseOut先快后慢

kCAMediaTimingFunctionEaseInEaseOut先慢后快再慢

kCAMediaTimingFunctionDefault实际效果是动画中间比较快.

2.用UIView的block回调实现动画的代码封装

1

2

3

4

5

6

7

8 #pragmaUIView实现动画

-(void)animationWithView:(UIView)viewWithAnimationTransition:(UIViewAnimationTransition)transition

{

[UIViewanimateWithDuration:DURATIONanimations:^{

[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIViewsetAnimationTransition:transitionforView:viewcache:YES];

}];

} 3.改变View的背景图,便于切换时观察

1

2

3

4

5 #pragma给View添加背景图

-(void)addBgImageWithImageName:(NSString)imageName

{

self.view.backgroundColor=[UIColorcolorWithPatternImage:[UIImageimageNamed:imageName]];

}

2.下面我们就开始编写点击button要回调的方法

(1).定义枚举来标示按钮所对应的动画类型,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18 typedefenum:NSUInteger{

Fade=,//淡入淡出

Push,//推挤

Reveal,//揭开

MoveIn,//覆盖

Cube,//立方体

SuckEffect,//吮吸

OglFlip,//翻转

RippleEffect,//波纹

PageCurl,//翻页

PageUnCurl,//反翻页

CameraIrisHollowOpen,//开镜头

CameraIrisHollowClose,//关镜头

CurlDown,//下翻页

CurlUp,//上翻页

FlipFromLeft,//左翻转

FlipFromRight,//右翻转

}AnimationType; 1

2 UIButtonbutton=sender;

AnimationTypeanimationType=button.tag;

(3).每次点击button都改变subtype的值,包括上,左,下,右

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21 NSStringsubtypeString;

switch(_subtype){

case:

subtypeString=kCATransitionFromLeft;

break;

case:

subtypeString=kCATransitionFromBottom;

break;

case:

subtypeString=kCATransitionFromRight;

break;

case:

subtypeString=kCATransitionFromTop;

break;

default:

break;

}

_subtype+=;

if(_subtype>){

_subtype=;

} (4),通过switch结合上边的枚举来判断是那个按钮点击的

1

2

3

4 switch(animationType)

{

//各种Case,此处代码下面会给出

} 1

2

3 caseFade:

[selftransitionWithType:kCATransitionFadeWithSubtype:subtypeStringForView:self.view];

break; 1

2

3 casePush:

[selftransitionWithType:kCATransitionPushWithSubtype:subtypeStringForView:self.view];

break;

效果如下:



(3).揭开效果:

1

2

3 caseReveal:

[selftransitionWithType:kCATransitionRevealWithSubtype:subtypeStringForView:self.view];

break; 果图如下:

(4).覆盖效果

1

2

3 caseMoveIn:

[selftransitionWithType:kCATransitionMoveInWithSubtype:subtypeStringForView:self.view];

break;

效果图如下:



(5).立方体效果

1

2

3 caseCube:

[selftransitionWithType:@"cube"WithSubtype:subtypeStringForView:self.www.visa158.comview];

break; 效果如下:

(6).吮吸效果

1

2

3 caseSuckEffect:

[selftransitionWithType:@"suckEffect"WithSubtype:subtypeStringForView:self.view];

break; 效果如下:



(7).翻转效果

1

2

3 caseOglFlip:

[selftransitionWithType:@"oglFlip"WithSubtype:subtypeStringForView:self.view];

break; 效果图如下:



8.波纹效果

1

2

3 caseRippleEffect:

[selftransitionWithType:@"rippleEffect"WithSubtype:subtypeStringForView:self.www.hunanwang.netview];

break; 1

2

3

4

5

6 casePageCurl:

[selftransitionWithType:@"pageCurl"WithSubtype:subtypeStringForView:self.view];

break;

casePageUnCurl:

[selftransitionWithType:@"pageUnCurl"WithSubtype:subtypeStringForView:self.view];

break;

(10).相机打开效果

1

2

3

4

5

6 caseCameraIrisHollowOpen:

[selftransitionWithType:@"cameraIrisHollowOpen"WithSubtype:subtypeStringForView:self.view];

break;

caseCameraIrisHollowClose:

[selftransitionWithType:@"cameraIrisHollowClose"WithSubtype:subtypeStringForView:self.view];

break;

(11),调用上面封装的第二个动画方法

1

2

3

4

5

6

7

8

9

10

11

12 caseCurlDown:

[selfanimationWithView:self.viewWithAnimationTransition:UIViewAnimationTransitionCurlDown];

break;

caseCurlUp:

[selfanimationWithView:self.viewWithAnimationTransition:UIViewAnimationTransitionCurlUp];

break;

caseFlipFromLeft:

[selfanimationWithView:self.viewWithAnimationTransition:UIViewAnimationTransitionFlipFromLeft];

break;

caseFlipFromRight:

[selfanimationWithView:self.viewWithAnimationTransition:UIViewAnimationTransitionFlipFromRight];

break;





















献花(0)
+1
(本文系白狐一梦首藏)