iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】
本文的最新版本已經(jīng)發(fā)布在簡(jiǎn)書[編程小翁]上,強(qiáng)烈建議到上查看簡(jiǎn)書,[點(diǎn)擊這里跳轉(zhuǎn)]。
在iOS中,圖形可分為以下幾個(gè)層次:
越上層,封裝程度越高,動(dòng)畫實(shí)現(xiàn)越簡(jiǎn)潔越簡(jiǎn)單,但是自由度越低;反之亦然。本文著重介紹Core Animation層的基本動(dòng)畫實(shí)現(xiàn)方案。
在iOS中,展示動(dòng)畫可以類比于顯示生活中的“拍電影”。拍電影有三大要素:演員+劇本+開拍,概念類比如下:
演員--->CALayer,規(guī)定電影的主角是誰(shuí) 劇本--->CAAnimation,規(guī)定電影該怎么演,怎么走,怎么變換 開拍--->AddAnimation,開始執(zhí)行一、概念介紹
1.1CALayer是什么呢?
CALayer是個(gè)與UIView很類似的概念,同樣有l(wèi)ayer,sublayer...,同樣有backgroundColor、frame等相似的屬性,我們可以將UIView看做一種特殊的CALayer,只不過(guò)UIView可以響應(yīng)事件而已。一般來(lái)說(shuō),layer可以有兩種用途,二者不互相沖突:一是對(duì)view相關(guān)屬性的設(shè)置,包括圓角、陰影、邊框等參數(shù),更詳細(xì)的參數(shù)請(qǐng)點(diǎn)擊這里;二是實(shí)現(xiàn)對(duì)view的動(dòng)畫操控。因此對(duì)一個(gè)view進(jìn)行core animation動(dòng)畫,本質(zhì)上是對(duì)該view的.layer進(jìn)行動(dòng)畫操縱。1.2CAAnimation是什么呢?
CAAnimation可分為四種:
- 1.CABasicAnimation
- 通過(guò)設(shè)定起始點(diǎn),終點(diǎn),時(shí)間,動(dòng)畫會(huì)沿著你這設(shè)定點(diǎn)進(jìn)行移動(dòng)。可以看做特殊的CAKeyFrameAnimation
- 2.CAKeyframeAnimation
- Keyframe顧名思義就是關(guān)鍵點(diǎn)的frame,你可以通過(guò)設(shè)定CALayer的始點(diǎn)、中間關(guān)鍵點(diǎn)、終點(diǎn)的frame,時(shí)間,動(dòng)畫會(huì)沿你設(shè)定的軌跡進(jìn)行移動(dòng)
- 3.CAAnimationGroup
- Group也就是組合的意思,就是把對(duì)這個(gè)Layer的所有動(dòng)畫都組合起來(lái)。PS:一個(gè)layer設(shè)定了很多動(dòng)畫,他們都會(huì)同時(shí)執(zhí)行,如何按順序執(zhí)行我到時(shí)候再講。
- 4.CATransition
- 這個(gè)就是蘋果幫開發(fā)者封裝好的一些動(dòng)畫
二、動(dòng)手干活
實(shí)踐出真知,看個(gè)例子就知道:
比如我們想實(shí)現(xiàn)一個(gè)類似心跳的縮放動(dòng)畫可以這么做,分為演員初始化、設(shè)定劇本、電影開拍三個(gè)步驟:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | - (void)initScaleLayer { ????//演員初始化 ????CALayer *scaleLayer = [[CALayer alloc] init]; ????scaleLayer.backgroundColor = [UIColor blueColor].CGColor; ????scaleLayer.frame = CGRectMake(60, 20 + kYOffset, 50, 50); ????scaleLayer.cornerRadius = 10; ????[self.view.layer addSublayer:scaleLayer]; ????[scaleLayer release]; ????? ????//設(shè)定劇本 ????CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; ????scaleAnimation.fromValue = [NSNumber?numberWithFloat:1.0]; ????scaleAnimation.toValue = [NSNumber?numberWithFloat:1.5]; ????scaleAnimation.autoreverses =?YES; ????scaleAnimation.fillMode = kCAFillModeForwards; ????scaleAnimation.repeatCount = MAXFLOAT; ????scaleAnimation.duration = 0.8; ????? ????//開演 ????[scaleLayer addAnimation:scaleAnimation forKey:@"scaleAnimation"]; } |
?
| 1 2 3 4 5 | - (void)viewDidLoad { ????[super?viewDidLoad]; ????// Do any additional setup after loading the view from its nib. ????[self?initScaleLayer]; } |
效果請(qǐng)參考附圖中的藍(lán)色方塊。其他效果可以依葫蘆畫瓢輕松實(shí)現(xiàn)。想要實(shí)現(xiàn)不同的效果,最關(guān)鍵的地方在于CABasicAnimation對(duì)象的初始化方式中keyPath的設(shè)定。在iOS中有以下幾種不同的keyPath,代表著不同的效果:
此外,我們還可以利用GroupAnimation實(shí)現(xiàn)多種動(dòng)畫的組合,在GroupAnimation中的各個(gè)動(dòng)畫類型是同時(shí)進(jìn)行的。
| - (void)initGroupLayer { ????//演員初始化 ????CALayer *groupLayer = [[CALayer alloc] init]; ????groupLayer.frame = CGRectMake(60, 340+100 + kYOffset, 50, 50); ????groupLayer.cornerRadius = 10; ????groupLayer.backgroundColor = [[UIColor purpleColor] CGColor]; ????[self.view.layer addSublayer:groupLayer]; ????[groupLayer release]; ??? ????//設(shè)定劇本 ????CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; ????scaleAnimation.fromValue = [NSNumber?numberWithFloat:1.0]; ????scaleAnimation.toValue = [NSNumber?numberWithFloat:1.5]; ????scaleAnimation.autoreverses = YES; ????scaleAnimation.repeatCount = MAXFLOAT; ????scaleAnimation.duration = 0.8; ????? ????CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; ????moveAnimation.fromValue = [NSValue?valueWithCGPoint:groupLayer.position]; ????moveAnimation.toValue = [NSValue?valueWithCGPoint:CGPointMake(320 - 80, ??????????????????????????????????????????????????????????????????groupLayer.position.y)]; ????moveAnimation.autoreverses = YES; ????moveAnimation.repeatCount = MAXFLOAT; ????moveAnimation.duration = 2; ????? ????CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"]; ????rotateAnimation.fromValue = [NSNumber?numberWithFloat:0.0]; ????rotateAnimation.toValue = [NSNumber?numberWithFloat:6.0 * M_PI]; ????rotateAnimation.autoreverses = YES; ????rotateAnimation.repeatCount = MAXFLOAT; ????rotateAnimation.duration = 2; ????? ????CAAnimationGroup *groupAnnimation = [CAAnimationGroup animation]; ????groupAnnimation.duration = 2; ????groupAnnimation.autoreverses = YES; ????groupAnnimation.animations = @[moveAnimation, scaleAnimation, rotateAnimation]; ????groupAnnimation.repeatCount = MAXFLOAT; ????//開演 ????[groupLayer addAnimation:groupAnnimation forKey:@"groupAnnimation"]; } |
| 1 2 3 4 5 | - (void)viewDidLoad { ????[super viewDidLoad]; ????// Do any additional setup after loading the view from its nib. ????[self initGroupLayer]; }? |
?完整的demo工程點(diǎn)CAAnimationDemo.zip下載?
=======================================================
by 編程小翁@博客園
from:?http://www.cnblogs.com/wengzilin/p/4250957.html
總結(jié)
以上是生活随笔為你收集整理的iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iOS下KVO使用过程中的陷阱
- 下一篇: UISegmentedControl的所