項(xiàng)目中涉及到一個(gè)動(dòng)畫,之前對(duì)動(dòng)畫很頭疼,感覺很難。然后百度+百度,算是了解了一點(diǎn)點(diǎn)。現(xiàn)在寫點(diǎn)心得,希望對(duì)你有幫助。
CABasicAnimation類的使用方式就是基本的關(guān)鍵幀動(dòng)畫。
所謂關(guān)鍵幀動(dòng)畫,就是將Layer的屬性作為KeyPath來注冊,指定動(dòng)畫的起始幀和結(jié)束幀,然后自動(dòng)計(jì)算和實(shí)現(xiàn)中間的過渡動(dòng)畫的一種動(dòng)畫方式。
CABasicAnimation的基本使用順序
1.引用QuartzCore.framework
將"QuartzCore.framework"這個(gè)庫添加到項(xiàng)目中。并且在需要使用CABaseAnimation類的地方import頭文件。項(xiàng)目需求,有時(shí)可能還需要引入"UIKit/UIKit"。
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
2.CABaseAnimation的實(shí)例化以及關(guān)鍵路徑的注冊
使用"animationWithKeyPath:"方法進(jìn)行CABasicAnimation的實(shí)例化,并指定Layer的屬性作為關(guān)鍵路徑來注冊。
// 指定strokeEnd屬性
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
3.設(shè)定動(dòng)畫
設(shè)定動(dòng)畫的屬性,一般常用的。
duration:動(dòng)畫時(shí)長
repeatCount:重復(fù)次數(shù)
fromValue:開始值
toValue:結(jié)束值
autoreverses:動(dòng)畫結(jié)束時(shí)是否執(zhí)行逆動(dòng)畫
4.實(shí)例
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface AnimationLineTool : NSObject
/**
* 通過設(shè)置點(diǎn),劃其運(yùn)動(dòng)軌跡
*
* @param pointA 點(diǎn)A
* @param pointB 點(diǎn)B
* @param pointC 點(diǎn)C
* @param view 當(dāng)前哪個(gè)View
*/
+ (void)moveA:(CGPoint)pointA andMoveB:(CGPoint)pointB andMoveC:(CGPoint)pointC andView:(UIView *)view;
/**
* 放大的動(dòng)畫
*
* @param view 添加到哪個(gè)view
*/
+(void)circleScaleAndZoomInView:(UIView *)view;
/**
* 移動(dòng)速度的動(dòng)畫
*
* @param fromValue 開始點(diǎn)
* @param toValue 結(jié)束點(diǎn)
* @param duration 運(yùn)行時(shí)間
* @return 速度軌跡
*/
+ (CABasicAnimation *)animationSpeedViewWithFromValue:(id)fromValue andToValue:(id)toValue andTimeDuration:(int)duration;
@end
#import "AnimationLineTool.h"
@implementation AnimationLineTool
+ (void)moveA:(CGPoint)pointA andMoveB:(CGPoint)pointB andMoveC:(CGPoint)pointC andView:(UIView *)view{
CAShapeLayer *linePath = [CAShapeLayer layer];
linePath=[CAShapeLayer layer];
linePath.lineCap=kCALineCapRound;
linePath.lineJoin=kCALineJoinBevel;
linePath.lineWidth=1;
linePath.fillColor=[UIColor clearColor].CGColor;
[view.layer addSublayer:linePath];
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 0.7;
path.lineCapStyle = kCGLineCapRound;//線條拐角
path.lineJoinStyle = kCGLineCapRound;//線條終點(diǎn)
[path moveToPoint:pointA];
[path addLineToPoint:pointB];
[path addLineToPoint:pointC];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 0.5;
pathAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue=[NSNumber numberWithFloat:0.0f];
pathAnimation.toValue=[NSNumber numberWithFloat:1.0f];
pathAnimation.autoreverses=NO;
linePath.path=path.CGPath;
//線條顏色
linePath.strokeColor=[UIColor orangeColor].CGColor;
[linePath addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
linePath.strokeEnd = 1.0;
}
+(void)circleScaleAndZoomInView:(UIView *)view{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//動(dòng)畫持續(xù)時(shí)間
animation.duration = 0.3;
// 動(dòng)畫結(jié)束時(shí)執(zhí)行逆動(dòng)畫
animation.autoreverses = NO;
// 開始時(shí)的倍率
animation.fromValue = [NSNumber numberWithFloat:-0.5];
// 結(jié)束時(shí)的倍率
animation.toValue = [NSNumber numberWithFloat:-1.0];
// 添加動(dòng)畫
[view.layer addAnimation:animation forKey:@"scale-layer"];
}
+ (CABasicAnimation *)animationSpeedViewWithFromValue:(id)fromValue andToValue:(id)toValue andTimeDuration:(int)duration
{
CABasicAnimation *animation2 = [CABasicAnimation animation];
animation2.keyPath = @"position.x";
animation2.fromValue = fromValue;
animation2.toValue = toValue;
animation2.duration = duration;
//兩個(gè)view的運(yùn)動(dòng)速率
animation2.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0 :0.7:0 :0.7];
animation2.fillMode = kCAFillModeForwards;
animation2.removedOnCompletion = NO;
return animation2;
}
@end
這是項(xiàng)目中,寫的一個(gè)連續(xù)動(dòng)畫類。你可以參照或者修改用來滿足你的需求。

調(diào)用上面的類方法,可以實(shí)現(xiàn)上邊的動(dòng)畫。具體實(shí)現(xiàn)可以自己嘗試寫一下,注意把握延遲時(shí)間的控制。已寫好DEMO,上傳GitHub,下載地址。內(nèi)含上邊的動(dòng)畫,還有閃爍,移動(dòng),縮放旋轉(zhuǎn),組合動(dòng)畫,線的運(yùn)動(dòng)軌跡。里邊的布局可能涉及到PureLayout,具體可以參考另一篇文章PureLayout的使用方法。