CABasicAnimation-自定義動(dòng)畫

項(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的使用方法。

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

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

  • iOS 蘋果官方Demo合集 字?jǐn)?shù)10517閱讀21059評(píng)論18喜歡144 其實(shí), 開發(fā)了這么久, 不得不說, ...
    bingo居然被占了閱讀 10,589評(píng)論 2 31
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,269評(píng)論 4 61
  • 每接一個(gè)電話,淚水就不聽使喚的落下。所有的不需要你懂,成了如今一切看不見的未來.......多想就這么走了,可生命...
    寒梅待開閱讀 158評(píng)論 0 1
  • 15年步入12月,16年開始好像一切又能從新來過,本就不是文藝的人,卻在這頗有轉(zhuǎn)折的一年里文藝的要死,本是充滿希...
    小情緒無格調(diào)閱讀 237評(píng)論 0 1

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