
核心動畫是一套包含圖形繪制,投影,動畫的Objective–C類集合。它通過開發人員所熟悉的應用程序套件和Cocoa Touch視圖架構的抽象分層模式,同時使用先進的合作效果提供了一套流暢的動畫。
使用核心動畫,你只需要設置一些參數比如起點和終點,剩下的幀核心動畫為你自動完成。
核心動畫類有以下分類:
- 提供顯示內容的圖層類。
- 動畫和計時類。
- 布局和約束類。
- 事務類,在原子更新的時候組合圖層類。
核心動畫的基礎類包含在Quartz核心框架(Quartz Core framework)里面。
圖層類是核心動畫的核心基礎,它提供了一套抽象的概念(假如你使用過NSView或者UIView的話,你一定會對它很熟悉)。CALayer是整個圖層類的基礎,它是所有核心動畫圖層類的父類。
雖然核心動畫的圖層和 Cocoa的視圖在很大程度上沒有一定的相似性,但是他們兩者最大的區別是,圖層不會直接渲染到屏幕上。
在模型-視圖-控制器(model-view-controller)概念里面NSView和UIView是典型的視圖部分,但是在核心動畫里面圖層是模型部分。圖層封裝了幾何、時間、可視化屬性,同時它提供了圖層現實的內容,但是實際顯示的過程則不是由它來完成。
圖層樹是核心動畫里面類似Cocoa視圖的層次結構。比如一個NSView或者UIView的實例擁有父視圖(superview)和子視圖(subview),一個核心動畫的圖層擁有父圖層(suplayer)和子圖層(sublayer)
動畫類:
一個基礎動畫僅僅需要要你指定一個開始的值和一個結束的值,或者目前的值,關鍵幀動畫可以使你指定:
每個關鍵幀的一個數組值,例如,你可以給一個CGColorRef對象的一個數組,用來改變背景的顏色。
在0.0和1.0之間的一個數組值,用來給每個幀指定總時間中所占的百分比的時間。
一個時間功能對象的數組(CAMediaTimingFunction),用來指定每幀動畫的時間步調。這個區域會使用一個預設的值來指定,例如kCAMediaTimingFunctionEaseIn,動畫出來時慢,然后加速。
關鍵幀動畫真的相當的簡單。首先,你需要考慮的是,成功的使用關鍵幀動畫,你需要做的基本步驟:
1.決定你想要做動畫的屬性(例如,框架,背景,錨點,位置,邊框,等等)
2.在動畫對象值的區域中,指定開始,結束,和中間的值。這些都是你的關鍵幀(看清單4-2)
3.使用duration這個字段指定動畫的時間
4.通常來講,通過使用times這個字段,來給每幀動畫指定一個時間。如果你沒有指定這些,核心動畫就會通過你在values這個字段指定的值分割出時間段。
5.通常,指定時間功能來控制步調。
這些都是你需要做的。你創建你的動畫和增加他們到層中。調用-addAnimation就開始了動畫。
關鍵幀動畫的時間:
[animation setCalculationMode:kCAAnimationLinear]; [animation setKeyTimes: [NSArray arrayWithObjects: [NSNumbernumberWithFloat:0.0], [NSNumbernumberWithFloat:0.25], [NSNumber numberWithFloat:0.50], [NSNumbernumberWithFloat:0.75], [NSNumbernumberWithFloat:1.0], nil]];
CAAnimationGroup *animGroup = [CAAnimationGroup animation]; animGroup.animations = [NSArray arrayWithObjects:moveAnim,scaleAnim,opacityAnim, nil]; animGroup.duration = 1; [view.layer addAnimation:animGroup forKey:nil];
隱式動畫:
/假設layer當前position為(100.0,100.0) theLayer.position=CGPointMake(500.0,500.0);
顯式動畫:
CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; theAnimation.duration=3.0; theAnimation.repeatCount=2; theAnimation.autoreverses=YES; theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; theAnimation.toValue=[NSNumber numberWithFloat:0.0]; [theLayer addAnimation:theAnimation forKey:@"animateOpacity"];
CALayer:
CALayer是個簡單的類,它是用來在屏幕上顯示內容展示的矩形區域。每個UIView都有一個根CALayer,UIView在這個layer上描繪東西。
CALayer *myLayer = myView.layer; //先添加QuartzCore庫,才能使用CALayer。 myLayer.backgroundColor = [UIColor orangeColor].CGColor; myLayer.cornerRadius = 20.0; CALayer *sublayer = [CALayer layer]; sublayer.backgroundColor = [UIColor purpleColor].CGColor; sublayer.shadowOffset = CGSizeMake(0, 3); sublayer.shadowRadius = 5.0; sublayer.shadowColor = [UIColor blackColor].CGColor; sublayer.shadowOpacity = 0.8; sublayer.frame = CGRectMake(30, 30, 128, 192); [self.view.layer addSublayer:sublayer]; CALayer *sublayer = [CALayer layer]; CALayer *imageLayer = [CALayer layer]; imageLayer.frame = sublayer.bounds; imageLayer.cornerRadius = 10.0; imageLayer.contents = (id)[UIImage imageNamed:@"snaguosha.png"].CGImage; imageLayer.masksToBounds = YES; [sublayer addSublayer:imageLayer];
CABasicAnimation animationWithKeyPath 一些規定的值:
廢話說的有點多,以下是常用的動畫:
1、按路徑移動:
[self.imageView.layer addAnimation:self.pathAnimation forKey:nil]; - (CAAnimation*)pathAnimation; { CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path,NULL,50.0,120.0); CGPathAddLineToPoint(path, NULL, 300, 488); // CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,150.0,120.0); // CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,250.0,120.0); // CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,350.0,120.0); // CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,450.0,120.0); CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [animation setPath:path]; [animation setDuration:3.0]; // [animation setAutoreverses:YES]; CFRelease(path); return animation; }
- (CAAnimation*)valuesAnimation; { CGPoint pt0 = CGPointMake(50.0, 120.0); CGPoint pt1 = CGPointMake(50.0, 275.0); CGPoint pt2 = CGPointMake(150.0, 275.0); CGPoint pt3 = CGPointMake(150.0, 120.0); CGPoint pt4 = CGPointMake(150.0, 275.0); CGPoint pt5 = CGPointMake(250.0, 275.0); CGPoint pt6 = CGPointMake(250.0, 120.0); CGPoint pt7 = CGPointMake(250.0, 275.0); CGPoint pt8 = CGPointMake(350.0, 275.0); CGPoint pt9 = CGPointMake(350.0, 120.0); CGPoint pt10 = CGPointMake(350.0, 275.0); CGPoint pt11 = CGPointMake(450.0, 275.0); CGPoint pt12 = CGPointMake(450.0, 120.0); NSArray *values = [NSArray arrayWithObjects: [NSValue valueWithCGPoint:pt0], [NSValue valueWithCGPoint:pt1], [NSValue valueWithCGPoint:pt2], [NSValue valueWithCGPoint:pt3], [NSValue valueWithCGPoint:pt4], [NSValue valueWithCGPoint:pt5], [NSValue valueWithCGPoint:pt6], [NSValue valueWithCGPoint:pt7], [NSValue valueWithCGPoint:pt8], [NSValue valueWithCGPoint:pt9], nil]; CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [animation setValues:values]; [animation setDuration:3.0]; [animation setAutoreverses:YES]; return animation; } [self.imageView.layer addAnimation:self.valuesAnimation forKey:nil];
2、縮放:
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; //x,y軸縮小到0.1,Z 軸不變 scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]; scaleAnim.removedOnCompletion = YES; [self.imageView.layer addAnimation:scaleAnim forKey:nil];
3、透明度變化:
//透明度變化 CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"]; opacityAnim.fromValue = [NSNumber numberWithFloat:1.0]; opacityAnim.toValue = [NSNumber numberWithFloat:0.1]; opacityAnim.removedOnCompletion = YES; opacityAnim.duration = 3.0f; [self.imageView.layer addAnimation:opacityAnim forKey:nil];
4、組合:
CAAnimationGroup *animGroup = [CAAnimationGroup animation]; animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil]; animGroup.duration = 1; [self.imageView.layer addAnimation:opacityAnim forKey:nil];
5、旋轉:
CABasicAnimation *TransformAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; TransformAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; //沿Z軸旋轉 TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,0,1)]; TransformAnim.cumulative = YES; TransformAnim.duration =3; //旋轉2遍,360度 TransformAnim.repeatCount =2; [self.imageView.layer addAnimation:TransformAnim forKey:nil];
