動畫分隱式動畫和顯式動畫
CAAnimatione采用了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