iOS 核心動畫

一、CABasicAnimation
二、CAKeyframeAnimation
三、CATransition
四、CAAnimationGroup

一、CABasicAnimation

// 1.創(chuàng)建動畫對象
CABasicAnimation *anim = [CABasicAnimation animation];

// 2.設(shè)置動畫對象
// keyPath決定了執(zhí)行怎樣的動畫, 調(diào)整哪個屬性來執(zhí)行動畫
// position bounds transform
anim.keyPath = @"position";
// anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, -1, 0)];
// anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
// toValue : 最終變成什么值
// byValue : 增加多少值
anim.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
anim.duration = 2.0;

/**讓圖層保持動畫執(zhí)行完畢后的狀態(tài)**/
// 動畫執(zhí)行完畢后不要刪除動畫
anim.removedOnCompletion = NO;
// 保持最新的狀態(tài)
anim.fillMode = kCAFillModeForwards;

// 3.添加動畫
[self.layer addAnimation:anim forKey:nil];

二、CAKeyframeAnimation

CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

anim.keyPath = @"position";

NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];
anim.values = @[v1, v2, v3, v4];

//    anim.keyTimes = @[@(0.5), @(0.25), @(0.25)];

anim.duration = 2.0;

anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;

[self.redView.layer addAnimation:anim forKey:nil];

//路徑
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

anim.keyPath = @"position";
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.duration = 2.0;

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
anim.path = path;
CGPathRelease(path);

// 設(shè)置動畫的執(zhí)行節(jié)奏
// kCAMediaTimingFunctionEaseInEaseOut : 一開始比較慢, 中間會加速,  臨近結(jié)束的時候, 會變慢
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.delegate = self;

[self.redView.layer addAnimation:anim forKey:nil];



#pragma mark - 動畫的代理方法
// 動畫開始的時候調(diào)用
- (void)animationDidStart:(CAAnimation *)anim
{
}
// 動畫結(jié)束的時候調(diào)用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
}
例:圖標(biāo)抖動
#define Angle2Radian(angle) ((angle) / 180.0 * M_PI)


CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";

anim.values = @[@(Angle2Radian(-5)),  @(Angle2Radian(5)), @(Angle2Radian(-5))];
anim.duration = 0.25;
// 動畫的重復(fù)執(zhí)行次數(shù)
anim.repeatCount = MAXFLOAT;

// 保持動畫執(zhí)行完畢后的狀態(tài)
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;

[self.iconView.layer addAnimation:anim forKey:@"shake"];

// 移除
[self.iconView.layer removeAnimationForKey:@"shake"];

三、CATransition

CATransition *anim = [CATransition animation];
anim.type = @"pageCurl";
//    anim.subtype = kCATransitionFromRight; //動畫方向
anim.duration = 0.5;
    
//    anim.startProgress = 0.0;
//    anim.endProgress = 0.5;

// type 
 *  @"cube"                     立方體翻滾效果
 *  @"moveIn"                   新視圖移到舊視圖上面
 *  @"reveal"                   顯露效果(將舊視圖移開,顯示下面的新視圖)
 *  @"fade"                     交叉淡化過渡(不支持過渡方向)             (默認(rèn)為此效果)
 *  @"pageCurl"                 向上翻一頁
 *  @"pageUnCurl"               向下翻一頁
 *  @"suckEffect"               收縮效果,類似系統(tǒng)最小化窗口時的神奇效果(不支持過渡方向)
 *  @"rippleEffect"             滴水效果,(不支持過渡方向)
 *  @"oglFlip"                  上下左右翻轉(zhuǎn)效果
 *  @"rotate"                   旋轉(zhuǎn)效果
 *  @"push"
 *  @"cameraIrisHollowOpen"     相機(jī)鏡頭打開效果(不支持過渡方向)
 *  @"cameraIrisHollowClose"    相機(jī)鏡頭關(guān)上效果(不支持過渡方向)

四、CAAnimationGroup

// 1.創(chuàng)建旋轉(zhuǎn)動畫對象
CABasicAnimation *rotate = [CABasicAnimation animation];
rotate.keyPath = @"transform.rotation";
rotate.toValue = @(M_PI);

// 2.創(chuàng)建縮放動畫對象
CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath = @"transform.scale";
scale.toValue = @(0.0);

// 3.平移動畫
CABasicAnimation *move = [CABasicAnimation animation];
move.keyPath = @"transform.translation";
move.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

// 4.將所有的動畫添加到動畫組中
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[rotate, scale, move];
group.duration = 2.0;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;

[self.myvie.layer addAnimation:group forKey:nil];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、基本概念 CoreAnimation 可用在 Mac OS X和iOS平臺 CoreAnimation 的動畫...
    Mitchell閱讀 2,517評論 1 16
  • Core Animation,中文翻譯為核心動畫,它是一組非常強(qiáng)大的動畫處理API,使用它能做出非常炫麗的動畫效果...
    lityjey閱讀 301評論 0 0
  • 上一章介紹了隱式動畫的概念。隱式動畫是在iOS平臺創(chuàng)建動態(tài)用戶界面的一種直接方式,也是UIKit動畫機(jī)制的基礎(chǔ),不...
    努力奔跑的小男孩閱讀 1,134評論 0 1
  • 核心動畫(Core Animation) 一、Core animation簡單介紹 1.Core Animatio...
    亦晴工作室閱讀 577評論 0 0
  • 寫在開始 2016年的歲末,2017年的歲初,選擇來到平遙,與一位朋友同行。古城平遙,過往并無過多認(rèn)識,說起來只能...
    九天飛飛飛閱讀 381評論 0 0

友情鏈接更多精彩內(nèi)容