一. 實(shí)現(xiàn)動(dòng)畫基本步驟
- 創(chuàng)建動(dòng)畫對(duì)象
- 設(shè)置動(dòng)畫屬性
- 把動(dòng)畫對(duì)象添加到某個(gè) CALayer 對(duì)象上
- 需要停止動(dòng)畫:可以調(diào)用 remove 方法移除動(dòng)畫
二. iOS 中核心動(dòng)畫種類
-
基本動(dòng)畫(CABasicAnimation)
- 平移動(dòng)畫
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"]; // 動(dòng)畫持續(xù)1秒 anim.duration = 1; // 因?yàn)镃GPoint是結(jié)構(gòu)體咒林,所以用NSValue包裝成一個(gè)OC對(duì) anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)]; anim.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; [layer addAnimation:anim forKey:@"MyAnim"]; // 通過MyAnim可以取回相應(yīng)的動(dòng)畫對(duì)象熬拒,比如用來中途取消動(dòng)畫
- 縮放動(dòng)畫
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
// 沒有設(shè)置fromValue說明當(dāng)前狀態(tài)作為初始值
// 寬度(width)變?yōu)樵瓉淼?倍,高度(height)變?yōu)樵瓉淼?.5倍
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 1.5, 1)];
anim.duration = 1;
[layer addAnimation:anim forKey:nil];
```
* 旋轉(zhuǎn)動(dòng)畫
```objc
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
// 這里是以向量(1, 1, 0)為軸映九,旋轉(zhuǎn)π/2弧度(90°)
// 如果只是在手機(jī)平面上旋轉(zhuǎn)梦湘,就設(shè)置向量為(0, 0, 1),即Z軸
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 1, 1, 0)];
anim.duration = 1;
[layer addAnimation:anim forKey:nil];
// 1. 創(chuàng)建核心動(dòng)畫的對(duì)象
CABasicAnimation* anim = [[CABasicAnimation alloc] init];
// 2. 設(shè)置核心動(dòng)畫的操作
anim.keyPath = @"transform.rotation";
// 從哪
anim.fromValue = @(100);
// 到哪
anim.toValue = @(300);
anim.byValue = @(M_PI_4);
// 動(dòng)畫完畢以后不回到原來的位置
anim.fillMode = kCAFillModeForwards;
anim.removedOnCompletion = NO;
// 3. 添加到layer上
[self.layer addAnimation:anim forKey:nil];
```
-
關(guān)鍵幀動(dòng)畫(CAKeyframeAnimation)
- 關(guān)鍵幀動(dòng)畫:有兩種形式
- 第一種是values的形式,
- 第二種是path路徑的形式;
- 關(guān)鍵幀動(dòng)畫:有兩種形式
-
組動(dòng)畫(CAAnimationGroup)
- 組動(dòng)畫: CAAnimation的子類,可以保存一組動(dòng)畫對(duì)象捌议,將CAAnimationGroup對(duì)象加入層后哼拔,組中所有動(dòng)畫對(duì)象可以同時(shí)并發(fā)運(yùn)行
- 屬性解析:animations:用來保存一組動(dòng)畫對(duì)象的NSArray
默認(rèn)情況下,一組動(dòng)畫對(duì)象是同時(shí)運(yùn)行的瓣颅,也可以通過設(shè)置動(dòng)畫對(duì)象的beginTime屬性來更改動(dòng)畫的開始時(shí)間
-
轉(zhuǎn)場(chǎng)動(dòng)畫(CATransition)
- 轉(zhuǎn)場(chǎng)動(dòng)畫: CAAnimation的子類倦逐,用于做轉(zhuǎn)場(chǎng)動(dòng)畫,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫效果
除了核心動(dòng)畫之外另外還有
圖片幀動(dòng)畫:具體如下UIImageView可以讓一系列的圖片在特定的時(shí)間內(nèi)按順序顯示
-
相關(guān)屬性解析:
- animationImages:要顯示的圖片(一個(gè)裝著UIImage的NSArray)
- animationDuration:完整地顯示一次animationImages中的所有圖片所需的時(shí)間
- animationRepeatCount:動(dòng)畫的執(zhí)行次數(shù)(默認(rèn)為0宫补,代表無限循環(huán))相關(guān)方法解析:
// 動(dòng)畫的一些操作
- (void)startAnimating; 開始動(dòng)畫
- (void)stopAnimating; 停止動(dòng)畫
- (BOOL)isAnimating; 是否正在運(yùn)行動(dòng)畫
未完待續(xù).........