核心动画

//  CoreAnimationDemo

//    CATransaction事务动画(显示动画(开头和结尾)和隐式动画)

//    CAAnimation是一个基类定义一些动画的基本属性和方法

//    CAPropertyAnimation属相动画是一个抽象的子类,支持动画的显示图层的关键路径(KeyPath)中定制的属性

//    CABasicAnimation基础动画简单的为图层属性提供修改

//    CAAnimationGroup组动画

//    CAKeyframeAnimation关键帧动画

//    CATransition过渡动画

//动画结束不移除动画效果(不返回)

CAAnimation *caa = nil;

caa.removedOnCompletion = NO;

caa.fillMode = kCAFillModeForwards;

CABasicAnimation *base;

//在你原来的基础上继续动画

base.cumulative =YES;

//事务动画

- (void)doTranscationAnimation {

//事务分为显示和隐式

//隐式事务指的是我们不去控制它的发生过程,只关心结果的时候,系统默认帮我们添加的一个动画的过程

//显示事务指的是我们明确的控制动画的发生过程

//显示动画

//开始

[CATransaction begin];

//动画时间

[CATransaction setAnimationDuration:0.5];

//速率函数

[CATransactionsetAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

//动画(图层)动画体

if(_layer.cornerRadius >= 30) {

_layer.cornerRadius = 10;

_layer.backgroundColor = [UIColor orangeColor].CGColor;

}else{

_layer.cornerRadius = 50;

_layer.backgroundColor= [UIColor blueColor].CGColor;

}

//提交

[CATransaction commit];

}

/*

keyPath::::::::::

边框颜色:borderColor

旋转:transform.rotation.ztransform.rotation.z

缩放:transform.scale

位置:position

背景颜色:backgroundColor

*/

//基础动画

- (void)doBaseAnimation {

//    CABasicAnimation

//创建基础动画对象

CABasicAnimation *borederColorAnimation = [CABasicAnimation animation];

//设置关键路径(做什么动画)

borederColorAnimation.keyPath = @"borderColor";

//动画时间

borederColorAnimation.duration= 0.5f;

//动画的值

borederColorAnimation.byValue= (id)[UIColor blueColor].CGColor;

//是否自动重复

borederColorAnimation.autoreverses =YES;

//设置重复次数

borederColorAnimation.repeatCount= 5;

//把动画添加到动画对象上

//    [_layer addAnimation:borederColorAnimation forKey:@"borederColor_Animation"];

//旋转

CABasicAnimation *rotationAnimotion = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

rotationAnimotion.duration= 1.5;

rotationAnimotion.toValue= @(M_PI);

rotationAnimotion.autoreverses=YES;

//    [_layer addAnimation:rotationAnimotion forKey:@"rotation_Animotion"];

//缩放

CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

scaleAnimation.duration= 1.5;

scaleAnimation.toValue= @(2.0);

scaleAnimation.autoreverses=YES;

//    [_layer addAnimation:scaleAnimation forKey:@"scale_Animation”];

//组动画

CAAnimationGroup *group = [CAAnimationGroup animation];

group.animations= @[borederColorAnimation,rotationAnimotion,scaleAnimation];

group.duration=3;

[_layer addAnimation:groupforKey:@"Group_Animaton"];

}

//关键帧动画

- (void)doKeyFrameAnimation {

CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

positionAnimation.duration= 2;

//动画过程的设置

//获得原始数据获取图层位置

CGPointpoint =_layer.position;

//设置过程

positionAnimation.values= @[[NSValuevalueWithCGPoint:point],[NSValuevalueWithCGPoint:CGPointMake(0, 400)],[NSValuevalueWithCGPoint:CGPointMake(300, 400)],[NSValuevalueWithCGPoint:point]];

//规定每个帧动画的时间从什么时候开始

positionAnimation.keyTimes= @[@0,@0.3,@0.5,@1];

//将动画添加个目标对象

[_layer addAnimation:position AnimationforKey:@"position_Animation"];

CAKeyframeAnimation *backgroundColorAnimation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];

backgroundColorAnimation.duration= 2;

backgroundColorAnimation.values= @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor];

[_layer addAnimation:backgroundColor AnimationforKey:@"backgroundColor_Animation"];

}

//过渡动画

- (void)doTransitionAnimation {

CATransition *transitionAnimation = [CATransition animation];

transitionAnimation.duration= 2;

//kCATransitionMoveIn

transitionAnimation.type=@"cube";

NSArray *subtypeArray = @[kCATransitionFromLeft,kCATransitionFromRight,kCATransitionFromBottom,kCATransitionFromTop,kCATransitionFromLeft];

transitionAnimation.subtype= subtypeArray[arc4random()%(subtypeArray.count)];

[_layer addAnimation:transition AnimationforKey:@"transition_Animation"];


/*

//transitionAnimation.type = @"oglFlip";

以下API效果可以安全使用

cube方块

suckEffect三角

rippleEffect水波抖动

pageCurl上翻页

pageUnCurl下翻页

oglFlip上下翻转

cameraIrisHollowOpen镜头快门开

cameraIrisHollowClose镜头快门开

以下API效果请慎用

spewEffect新版面在屏幕下方中间位置被释放出来覆盖旧版面.

genieEffect旧版面在屏幕左下方或右下方被吸走,显示出下面的新版面

unGenieEffect新版面在屏幕左下方或右下方被释放出来覆盖旧版面.

twist版面以水平方向像龙卷风式转出来.

tubey版面垂直附有弹性的转出来.

swirl旧版面360度旋转并淡出,显示出新版面.

charminUltra旧版面淡出并显示新版面.

zoomyIn新版面由小放大走到前面,旧版面放大由前面消失.

zoomyOut新版面屏幕外面缩放出现,旧版面缩小消失.

oglApplicationSuspend像按”home”按钮的效果.

*/

}


结构字段的键的取值

CAAnimation供支持使用关键路径访问选择的结构字段。这在为动画关键路径指定结构字段的时候非常有帮助,同时你可以使用setValue:forKeyPath:和valueForKeyPath来设置和读取相应的值。

CATransform3D公开如下的字段:

结构体字段                          描述

transform.rotation.x              The rotation, in radians, in the x axis.

transform.rotation.y              The rotation, in radians, in the y axis.

transform.rotation.z            The rotation, in radians, in the z axis.

transform.rotation                  The rotation, in radians, in the z axis. This is identical to setting the rotation.z field.

transform.scale.x                  Scale factor for the x axis.

transform.scale.y                  Scale factor for the y axis.

transform.scale.z                 Scale factor for the z axis.

transform.scale                   Average of all three scale factors.

transform.translation.x          Translate in the x axis.

transform.translation.y           Translate in the y axis.

translation.z                          Translate in the z axis.

translation                            Translate in the x and y axis. Value is an NSSize or CGSize.

CGPoint公开如下字段:

结构体字段描述

x                          The x component of the point.

y                           The y component of the point.

CGSize公开如下字段:

结构体字段描述

width                      The width component of the size.

height                     The height component of the size.

CGRect公开如下字段:

结构体字段描述

origin                      The origin of the rectangle as a CGPoint.

origin.x                    The x component of the rectangle origin.

origin.y                     The y component of the rectangle origin.

size                           The size of the rectangle as a CGSize.

size.width                  The width component of the rectangle size.

size.height                The height component of the rectangle size

你不可以通过Objective-C 2.0的属性方法来指定一个结构字段的关键路径。如下的代码是无法正常执行的:

myLayer.transform.rotation.x = 0;

相反你必须使用setValue:forKeyPath:或者valuForKeyPath:,如下:

[myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容