iOS核心动画 Core Animation2-CABasicAnimation
Core Animation2-CABasicAnimation
本文目錄- 一、平移動(dòng)畫(huà)
- 二、縮放動(dòng)畫(huà)
- 三、旋轉(zhuǎn)動(dòng)畫(huà)
- 四、其他
CABasicAnimation是CAPropertyAnimation的子類(lèi),使用它可以實(shí)現(xiàn)一些基本的動(dòng)畫(huà)效果,它可以讓CALayer的某個(gè)屬性從某個(gè)值漸變到另一個(gè)值。下面就用CABasicAnimation實(shí)現(xiàn)幾個(gè)簡(jiǎn)單的動(dòng)畫(huà)。
* 先初始化一個(gè)UIView添加到控制器的view中,然后在這個(gè)UIView的layer上執(zhí)行動(dòng)畫(huà),下面的self是指控制器
1 _myView = [[UIView alloc] init]; 2 _myView.layer.position = CGPointMake(100, 100); 3 _myView.layer.bounds = CGRectMake(0, 0, 100, 100); 4 _myView.backgroundColor = [UIColor blueColor]; 5 [self.view addSubview:_myView]; 6 [_myView release]; 回到頂部一、平移動(dòng)畫(huà)
實(shí)現(xiàn)平移動(dòng)畫(huà)有好幾種方法,這里列舉2種。
1.方法1
1 // 說(shuō)明這個(gè)動(dòng)畫(huà)對(duì)象要對(duì)CALayer的position屬性執(zhí)行動(dòng)畫(huà) 2 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"]; 3 // 動(dòng)畫(huà)持續(xù)1.5s 4 anim.duration = 1.5; 5 6 // position屬性值從(50, 80)漸變到(300, 350) 7 anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 80)]; 8 anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 350)]; 9 10 // 設(shè)置動(dòng)畫(huà)的代理 11 anim.delegate = self; 12 13 // 保持動(dòng)畫(huà)執(zhí)行后的狀態(tài) 14 anim.removedOnCompletion = NO; 15 anim.fillMode = kCAFillModeForwards; 16 17 // 添加動(dòng)畫(huà)對(duì)象到圖層上 18 [_myView.layer addAnimation:anim forKey:@"translate"];* 第2行設(shè)置的keyPath是@"position",說(shuō)明要修改的是CALayer的position屬性,也就是會(huì)執(zhí)行平移動(dòng)畫(huà)
* 注意第7、8行,這里并不是直接使用CGPoint這種結(jié)構(gòu)體類(lèi)型,而是要先包裝成NSValue對(duì)象后再使用。這2行代碼表示CALayer從位置(50, 80)移動(dòng)到位置(300, 350)
* 如果將第8行的toValue換成byValue,代表CALayer從位置(50, 80)開(kāi)始向右移動(dòng)300、向下移動(dòng)350,也就是移動(dòng)到位置(350, 430)
* 默認(rèn)情況下,動(dòng)畫(huà)執(zhí)行完畢后,動(dòng)畫(huà)會(huì)自動(dòng)從CALayer上移除,CALayer又會(huì)回到原來(lái)的狀態(tài)。為了保持動(dòng)畫(huà)執(zhí)行后的狀態(tài),可以加入第14、15行代碼
* 第18行后面的@"translate"是給動(dòng)畫(huà)對(duì)象起個(gè)名稱(chēng),以后可以調(diào)用CALayer的removeAnimationForKey:方法根據(jù)動(dòng)畫(huà)名稱(chēng)停止相應(yīng)的動(dòng)畫(huà)
* 第11行是設(shè)置動(dòng)畫(huà)的代理,可以監(jiān)聽(tīng)動(dòng)畫(huà)的執(zhí)行過(guò)程,這里設(shè)置控制器為代理。代理需要實(shí)現(xiàn)的方法有:
1 #pragma mark 動(dòng)畫(huà)開(kāi)始 2 - (void)animationDidStart:(CAAnimation *)anim { 3 NSLog(@"動(dòng)畫(huà)開(kāi)始了"); 4 } 5 6 #pragma mark 動(dòng)畫(huà)結(jié)束 7 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { 8 // 查看一下動(dòng)畫(huà)執(zhí)行完畢后的position值 9 NSString *string = NSStringFromCGPoint(_myView.layer.position); 10 NSLog(@"動(dòng)畫(huà)結(jié)束了,position:%@", string); 11 }打印結(jié)果為:
1 2013-04-14 23:44:26.197 CAAnimation[5995:c07] 動(dòng)畫(huà)開(kāi)始了 2 2013-04-14 23:44:27.697 CAAnimation[5995:c07] 動(dòng)畫(huà)結(jié)束了,position:{100, 100}從第2行的打印信息可以看出,實(shí)際上,動(dòng)畫(huà)執(zhí)行完畢后,并沒(méi)有真正改變CALayer的position屬性的值!
?
2.方法2
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"]; 2 anim.duration = 1; 3 4 CATransform3D form = CATransform3DMakeTranslation(350, 350, 0); 5 anim.toValue = [NSValue valueWithCATransform3D:form]; 6 7 [_myView.layer addAnimation:anim forKey:nil];通過(guò)CALayer的transform屬性實(shí)現(xiàn)平移動(dòng)畫(huà),layer會(huì)從自己的初始位置平移到(350, 350)位置
?
回到頂部二、縮放動(dòng)畫(huà)
實(shí)現(xiàn)縮放動(dòng)畫(huà)有好幾種方法,這里列舉2種。
1.方法1
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"]; 2 anim.duration = 2; 3 4 anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 30, 30)]; 5 6 [_myView.layer addAnimation:anim forKey:nil];layer會(huì)從原來(lái)的尺寸(100x100)變?yōu)?0x30
?
2.方法2
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"]; 2 anim.duration = 1.5; // 動(dòng)畫(huà)持續(xù)1.5s 3 4 // CALayer的寬度從0.5倍變?yōu)?倍 5 // CALayer的高度從0.5倍變?yōu)?.5倍 6 anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)]; 7 anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 1.5, 1)]; 8 9 [_myView.layer addAnimation:anim forKey:nil];?
回到頂部三、旋轉(zhuǎn)動(dòng)畫(huà)
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"]; 2 anim.duration = 1.5; 3 4 // 繞著(0, 0, 1)這個(gè)向量軸順時(shí)針旋轉(zhuǎn)45° 5 anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)]; 6 7 [_myView.layer addAnimation:anim forKey:nil];其實(shí)可以不用設(shè)置fromValue,這里只設(shè)置了toValue
?
回到頂部四、其他
* 除開(kāi)前面使用的position、transform屬性,其實(shí)CALayer還有好多屬性都可以形成動(dòng)畫(huà),這些屬性統(tǒng)稱(chēng)為"Animatable Properties"。在《CALayer3-層的屬性》開(kāi)頭有介紹如何搜索這些屬性
*?CABasicAnimation雖然能夠做很多基本的動(dòng)畫(huà)效果,但是有個(gè)局限性,只能讓CALayer的屬性從某個(gè)值漸變到另一個(gè)值,僅僅是在2個(gè)值之間漸變
總結(jié)
以上是生活随笔為你收集整理的iOS核心动画 Core Animation2-CABasicAnimation的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 汇丰银行信用卡还款日可以改吗?还款日当天
- 下一篇: iOS持久化存储-CoreData简介