CAAnimation动画/CAAnimation Group

动画分隐式动画和显式动画

CAAnimation采用了CAMediaTiming协议,可以调整时间,包括持续时间,速度,重复次数;采用了CAAction协议,可以通过响应动作的方式来显示动画.

CAAnimation的一些派生类:
CATransition 提供渐变效果:(推拉push效果,消退fade效果,揭开reveal效果)
CAAnimationGroup 允许多个动画同时播放
CABasicAnimation 提供了对单一动画的实现
CAKeyframeAnimation 关键桢动画,可以定义行动路线
CAConstraint 约束类,在布局管理器类中用它来设置属性
CAConstraintLayoutManager 约束布局管理器,是用来将多个CALayer进行布局的.各个CALayer是通过名称来区分,而布局属性是通过CAConstraint来设置的.
CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务.

事务管理(Transactions)
事务分两种:
1.隐式事务(implicit transaction)
除显式事务外,任何对于CALayer属性的修改,都是隐式事务.这样的事务会在run-loop中被提交.
如:
theLayer.opacity = 0.0;
theLayer.zPosition = -200;
theLayer.position = CGPointMake(0.0, 0.0);

2.显式事务(explicit transaction)
a. 通过明确的调用begin,commit来提交动画.优点是可以同时修改多个Layer的属性.
如:
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
                               forKey:kCATransactionDisableActions];
[aLayer removeFromSuperlayer];
[CATransaction commit];

b.可以重置持续时间
可以在begin,commit对中临时修改动画持续时间.
[CATransaction begin]
[CATransaction setValue:[NSNumber numberWithFloat:10.0f]
                               forKey:kCATransactionAnimationDuration];
theLayer.zPosition = 200.0;
theLayer.opacity = 0.0;
[CATransaction commit];

c.事务可以嵌套.
如:
//第一层嵌套
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0f]
                             forKey:kCATransactionAnimationDuration];
theLayer.position = CGPointMake(0.0, 0.0);
//第二层嵌套
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:5.0f]
                               forKey:kCATransactionAnimationDuration];
theLayer.zPosition = 200.0;
theLayer.opacity = 0.0;
[CATransaction commit];
[CATransaction commit];

布局管理器示例如下:
//创建和设置一个布局管理器
theLayer.layoutManager = [CAConstraintLayoutManager layoutManager];

//创建layerA
CALayer *layerA = [CALayer layer];
layerA.name = @"layerA";

//设置layerA的中点位置等于超类的中点位置
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY
                                                                                       relativeTo:@"superLayer"
                                                                                       attribute:kCAConstraintMidY]];

[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX
                                                                                       relativeTo:@"superLayer"
                                                                                       attribute:kCAConstraintMidX]];
[theLayer addSublayer:layerA];

//创建layerB
CALayer *layerB = [CALayer layer];
layerB.name = @"layerB";

//设置layerB的宽度等于layerA的宽度
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintWidth
                                                                                       relativeTo:@"LayerA"
                                                                                       attribute:kCAConstraintWidth]];

[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX
                                                                                       relativeTo:@"layerA"
                                                                                       attribute:kCAConstraintMidX]];

[theLayer addSublayer:layerB];

 

转自‘http://hi.baidu.com/iamgleaf/blog/item/38520d949d025107d31b7023.html

 

 

 

 

//  AnimationView.m

//  AnimationTestV2

//

//  Created by jtone  on 11-8-9.

//  Copyright 2011年__MyCompanyName__. All rights reserved.

//

#import"AnimationView.h"

#import<QuartzCore/QuartzCore.h>

#define kDuaitionOfTimer00.2

#define kDuaitionOfTimer10.2

#define kDuaitionOfTimer20.2

#define kDuaitionOfTimer30.2

#define kDuaitionOfTimer41.1

#define kDisplacementOfTimer110

#define kDisplacementOfTimer210

@implementationAnimationView

CGPoint leftPhoneCenter;

CGPoint contactCenter;

CGPoint rightPhoneCenter;

CGPoint picCenter;

//位置变化动画

- (CAAnimation *)animationMoveFrom:(CGPoint) from To:(CGPoint) to Duration:(CGFloat) duration BeginTime:(CGFloat)beginTime 

{

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

CGFloat animationDuration = duration;

CGMutablePathRef thePath = CGPathCreateMutable();

CGPathMoveToPoint(thePath, NULL, from.x, from.y);

CGPathAddLineToPoint(thePath, NULL, to.x, to.y);

bounceAnimation.path = thePath;

bounceAnimation.duration = animationDuration;

    bounceAnimation.beginTime = beginTime;

bounceAnimation.repeatCount=0;

bounceAnimation.removedOnCompletion=NO;

bounceAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

CGPathRelease(thePath);

return bounceAnimation;

}

//透明度变化动画

-(CAAnimation *)animationWithOpacityFrom:(CGFloat) from To:(CGFloat) to Duration:(CGFloat) duration BeginTime:(CGFloat)beginTime 

{    

    CABasicAnimation *theAnimation;    

    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];    

    theAnimation.duration=duration; 

    theAnimation.beginTime = beginTime;    

    theAnimation.repeatCount=0;    

    theAnimation.autoreverses=NO;    

    theAnimation.fromValue=[NSNumber numberWithFloat:from];    

    theAnimation.toValue=[NSNumber numberWithFloat:to];

    return theAnimation;

}

-(NSArray *)AnimWithPhone:(CGPoint)phoneCenter Option:(int)option//手机壳动画设置

{

    NSArray *arr = [NSArray arrayWithObjects:

                    [self   animationMoveFrom:phoneCenter  To:CGPointMake(phoneCenter.x+kDisplacementOfTimer1*option,phoneCenter.y)    Duration:kDuaitionOfTimer0 BeginTime:1.5],

                    [self   animationMoveFrom:CGPointMake(phoneCenter.x+kDisplacementOfTimer1*option,phoneCenter.y)    To:phoneCenter  Duration:kDuaitionOfTimer1 BeginTime:1.7],

                    [self   animationMoveFrom:phoneCenter    To:CGPointMake(phoneCenter.x+kDisplacementOfTimer2*option,phoneCenter.y)  Duration:kDuaitionOfTimer2 BeginTime:1.9],

                    [self  animationMoveFrom:CGPointMake(phoneCenter.x+kDisplacementOfTimer2*option,phoneCenter.y)    To:phoneCenter  Duration:kDuaitionOfTimer3 BeginTime:2.1],

                   nil];

   returnarr;

}

-(NSArray *)AnimFromObj:(CGPoint)obj1 ToObj:(CGPoint)obj2 Option:(int)option//图片,联系人动画设置

{

    NSArray *arr = [NSArray arrayWithObjects:

                    [self  animationWithOpacityFrom:0.0To:0.0Duration:1.0BeginTime:0.0],

                    [self  animationWithOpacityFrom:0.0To:1.0Duration:0.3BeginTime:1.0],

                    [self  animationMoveFrom:obj1  To:CGPointMake(obj1.x+kDisplacementOfTimer1*option, obj1.y)    Duration:kDuaitionOfTimer0 BeginTime:1.5],

                    [self  animationMoveFrom:CGPointMake(obj1.x+kDisplacementOfTimer1*option, obj1.y)    To:obj1  Duration:kDuaitionOfTimer1 BeginTime:1.7],

                    [self  animationMoveFrom:obj1    To:CGPointMake(obj1.x+kDisplacementOfTimer2*option,obj1.y)   Duration:kDuaitionOfTimer2 BeginTime:1.9],

                    [self  animationMoveFrom:CGPointMake(obj1.x+kDisplacementOfTimer2*option,obj1.y)     To:obj1  Duration:kDuaitionOfTimer3 BeginTime:2.1],

                    [self  animationMoveFrom:obj1 To:obj2 Duration:kDuaitionOfTimer4 BeginTime:2.4],

                    [self  animationMoveFrom:obj2 To:obj2 Duration:kDuaitionOfTimer4 BeginTime:3.5],

                    [self  animationWithOpacityFrom:1.0To:0.0Duration:0.3BeginTime:3.7],

                   nil];                    

   returnarr;

}

-(CAAnimationGroup *)getAnimGroup //创建动画组

{

    CAAnimationGroup * animGroup  = [CAAnimationGroup animation];

animGroup.delegate            = self;

animGroup.removedOnCompletion = NO;

animGroup.duration  = 4.0;

animGroup.repeatCount  = 1;

animGroup.fillMode  = kCAFillModeForwards;

    return animGroup;

}

-(void)setup//开始动画

{

    leftPhoneCenter    = CGPointMake(leftPhone.frame.origin.x+35, leftPhone.frame.origin.y+55);

    contactCenter         = CGPointMake(contact.frame.origin.x+25, contact.frame.origin.y+25);

    rightPhoneCenter = CGPointMake(rightPhone.frame.origin.x+35, rightPhone.frame.origin.y+55);

    picCenter                 = CGPointMake(picture.frame.origin.x+25,picture.frame.origin.y+25);

    contact.hidden = NO;

    picture.hidden = NO;

    CAAnimationGroup * mp1 = [self getAnimGroup];

    mp1.animations           = [self AnimWithPhone:leftPhoneCenter Option:-1];

    [leftPhone.layer addAnimation:mp1 forKey:@"jtone"];

    [leftPhoneScreen.layer addAnimation:mp1 forKey:@"jtone"];

    CAAnimationGroup * mp2 = [self getAnimGroup];

    mp2.animations           = [self AnimWithPhone:rightPhoneCenter Option:1];

    [rightPhone.layer addAnimation:mp2 forKey:@"jtone"];

    [rightPhoneScreen.layer addAnimation:mp2 forKey:@"jtone"];

    CAAnimationGroup * mp3 = [self getAnimGroup];

    mp3.animations           = [self AnimFromObj:contactCenter ToObj:picCenter Option:-1];

    [contact.layer addAnimation:mp3 forKey:@"jtone"];    

    CAAnimationGroup * mp4 = [self getAnimGroup];

    mp4.animations           = [self AnimFromObj:picCenter ToObj:contactCenter Option:1];

    [picture.layer addAnimation:mp4 forKey:@"jtone"];

}

- (void)dealloc

{

    [superdealloc];

}

@end

时间: 2024-08-07 00:25:33

CAAnimation动画/CAAnimation Group的相关文章

CAAnimation解读

CAAnimation解读 CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类: CATransition:提供渐变效果,如推拉push效果,消退fade效果,揭开reveal效果 CAAnimationGroup:允许多个动画同时播放 CABasicAnimation: 提供了对单一动画的实现 CAKeyframeAnimation: 关键桢动画,可以定义动画路线 CAPropertyAnim

iOS中Animation 动画 UI_22

版权声明:本文为博主原创文章,未经博主允许不得转载. 1.iOS中我们能看到的控件都是UIView的子类,比如UIButton UILabel UITextField UIImageView等等 2.UIView能够在屏幕的显示是因为在创建它的时候内部自动添加一个CALayer图层,通过这个图层在屏幕上显示的时候会调用一个drawRect: 的方法,完成绘图,才能在屏幕上显示 3.CALayer 本身就具有显示功能,但是它不能响应用户的交互事件,如果只是单纯的显示一个图形,此时你可以使用CALa

iOS中 Animation 动画大全 韩俊强的博客

每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 1.iOS中我们能看到的控件都是UIView的子类,比如UIButton UILabel UITextField UIImageView等等 2.UIView能够在屏幕的显示是因为在创建它的时候内部自动添加一个CALayer图层,通过这个图层在屏幕上显示的时候会调用一个drawRect: 的方法,完成绘图,才能在屏幕上显示 3.CALayer 本身就具有显示功能,但

IOS 30多个iOS常用动画,带详细注释

[cpp] view plaincopy //   //  CoreAnimationEffect.h   //  CoreAnimationEffect   //   //  Created by VincentXue on 13-1-19.   //  Copyright (c) 2013年 VincentXue. All rights reserved.   //       #import <Foundation/Foundation.h>       /**   !  导入Quart

iOS的CoreAnimation开发框架中的Layer层动画制作解析_IOS

CAAnimation动画体系的介绍CAAnimation是CoreAnimation框架中执行动画对象的基类,下面有一张图,是我手画的,不太美观,但是可以将与CAAnimation相关的几个动画类的关系表达清楚: 从上图中可以看到,从CAAnimation中继承出三个子类,分别是用于创建属性动画的CAPropertyAnimation,创建转场动画的CATransition和创建组合动画的CAAnimationGroup. 我们就先从根类开始探讨. 1.CAAnimation属性和方法 CAA

iOS开发CoreAnimation解读之四——Layer层动画内容

iOS开发CoreAnimation解读之四--Layer层动画内容 一.引言         通过前几篇博客的介绍,我们可以了解到layer层可以设置许多与控件UI相关的属性,并且对于iOS开发,UIView层的属性是会映射到CALayer的,因此,可以通过UIKit和CoreAnimation两个框架来设置控件的UI相关属性,当属性发生变化时,我们可以使其展示一个动画效果. 二.CAAnimation动画体系的介绍         CAAnimation是CoreAnimation框架中执行

动画

概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看到动画操作在iOS中是如何简单和高效,很多原来想做但是苦于没有思路的动画在iOS中将变得越发简单: CALayer CALayer简介 CALayer常用属性 CALayer绘图 Core Animat

【iOS开发】30多个iOS常用动画,带详细注释

30多个iOS常用动画,带详细注释 // // CoreAnimationEffect.h // CoreAnimationEffect // // Created by VincentXue on 13-1-19. // Copyright (c) 2013年 VincentXue. All rights reserved. // import   @interface CoreAnimationEffect : NSObject pragma mark - Custom Animation

详解iOS开发中的转场动画和组动画以及UIView封装动画_IOS

一.转场动画 CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果 属性解析: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点(在整体动画的百分比) 转场动画代码示例 1.界面搭建 2.实现代码 复制代码