iOS 核心動畫

Core Animation,中文翻譯為核心動畫,它是一組非常強大的動畫處理API,使用它能做出非常炫麗的動畫效果,而且往往是事半功倍。也就是說,使用少量的代碼就可以實現非常強大的功能。
Core Animation可以用在Mac OS X和iOS平臺。
Core Animation的動畫執行過程都是在后臺操作的,不會阻塞主線程。
要注意的是,Core Animation是直接作用在CALayer上的,并非UIView。

一 、Core Animation結構

其中灰色虛線表示繼承關系,紅色表示遵守協議
核心動畫中所有類都遵守CAMediaTiming協議

1.CAAnaimation

是個抽象類,不具備動畫效果,必須用它的子類才有動畫效果。是所有動畫對象的父類,負責控制動畫的持續時間和速度。

2.CAAnimationGroup和CATransition

是CAAnimation的子類,有動畫效果。
CAAnimationGroup是個動畫組,可以同時進行縮放,旋轉(同時進行多個動畫)。
CATransition是轉場動畫,界面之間跳轉(切換)都可以用轉場動畫。

3.CAPropertyAnimation

是CAAnimation的子類,也是個抽象類,本身不具備動畫效果,只有子類才有。要想創建動畫對象,應該使用它的兩個子類:CABasicAnimation和CAKeyframeAnimation。

4.CABasicAnimation和CAKeyframeAnimation

CABasicAnimation基本動畫,做一些簡單效果。
CAKeyframeAnimation幀動畫,做一些連續的流暢的動畫。

二、基本使用

以基本動畫為例:
先要有CALayer圖層。
初始化一個CABasicAnimation對象,給對象設置相關的屬性。
將基本動畫對象添加到CALayer對象中就可以開始動畫了。

CALayer *layer = [CALayer layer];
...
CABasicAnimation *animation = [CABasicAnimation animation];
anmation.keyPath = @"transform.scale";
anmation.toValue = @0;
[layer addAnimation:animation forKey:nil];

三、CAAnimation

1、CABasicAnimation(基礎動畫)

CAPropertyAnimation的子類
屬性解析:
fromValue:keyPath相應屬性的初始值
toValue:keyPath相應屬性的結束值
隨著動畫的進行,在長度為duration的持續時間內,keyPath相應屬性的值從fromValue漸漸地變為toValue
如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在動畫執行完畢后,圖層會保持顯示動畫執行后的狀態。但在實質上,圖層的屬性值還是動畫執行前的初始值,并沒有真正被改變。
比如,CALayer的position初始值為(0,0),CABasicAnimation的fromValue為(10,10),toValue為(100,100),雖然動畫執行完畢后圖層保持在(100,100)這個位置,實質上圖層的position還是為(0,0)

平移動畫

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //創建layer
    CALayer *myLayer=[CALayer layer];
    //設置layer的屬性
    myLayer.bounds=CGRectMake(0, 0, 50, 80);
    myLayer.backgroundColor=[UIColor yellowColor].CGColor;
    myLayer.position=CGPointMake(50, 50);
    myLayer.anchorPoint=CGPointMake(0, 0);
    myLayer.cornerRadius=20;
    //添加layer
    [self.view.layer addSublayer:myLayer];
    self.myLayer=myLayer;
}

//設置動畫(基礎動畫)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.創建核心動畫
    //    CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:<#(NSString *)#>]
    CABasicAnimation *anima=[CABasicAnimation animation];

    //1.1告訴系統要執行什么樣的動畫
    anima.keyPath=@"position";
    //設置通過動畫,將layer從哪兒移動到哪兒
    anima.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
    anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];

    //1.2設置動畫執行完畢之后不刪除動畫
    anima.removedOnCompletion=NO;
    //1.3設置保存動畫的最新狀態
    anima.fillMode=kCAFillModeForwards;
    anima.delegate=self;
    //打印
    NSString *str=NSStringFromCGPoint(self.myLayer.position);
    NSLog(@"執行前:%@",str);

    //2.添加核心動畫到layer
    [self.myLayer addAnimation:anima forKey:nil];

}

-(void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"開始執行動畫");
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //動畫執行完畢,打印執行完畢后的position值
    NSString *str=NSStringFromCGPoint(self.myLayer.position);
    NSLog(@"執行后:%@",str);
}
@end

縮放動畫

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.創建動畫
    CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"bounds"];
    //1.1設置動畫執行時間
    anima.duration=2.0;
    //1.2設置動畫執行完畢后不刪除動畫
    anima.removedOnCompletion=NO;
    //1.3設置保存動畫的最新狀態
    anima.fillMode=kCAFillModeForwards;
    //1.4修改屬性,執行動畫
    anima.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
    //2.添加動畫到layer
    [self.myLayer addAnimation:anima forKey:nil];
}

旋轉動畫

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.創建動畫
    CABasicAnimation *anima=[CABasicAnimation animationWithKeyPath:@"transform"];
    //1.1設置動畫執行時間
    anima.duration=2.0;
    //1.2修改屬性,執行動畫
    anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
    //1.3設置動畫執行完畢后不刪除動畫
    anima.removedOnCompletion=NO;
    //1.4設置保存動畫的最新狀態
    anima.fillMode=kCAFillModeForwards;

    //2.添加動畫到layer
    [self.myLayer addAnimation:anima forKey:nil];
}

2、CAKeyframeAnimation(關鍵幀動畫)

關鍵幀動畫,也是CAPropertyAnimation的子類,與CABasicAnimation的區別是:
CABasicAnimation只能從一個數值(fromValue)變到另一個數值(toValue),而CAKeyframeAnimation會使用一個NSArray保存這些數值
CABasicAnimation可看做是只有2個關鍵幀的CAKeyframeAnimation

修改位置

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 1.創建關鍵幀動畫
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    anim.duration = 2;
    // 2.設置屬性
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(300, 200)];
    NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(300, 500)];
    NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(100, 500)];


    anim.values = @[value1, value2, value3, value4, value1];

    // 3.添加到layer上
    [self.magLayer addAnimation:anim forKey:nil];
}

沿著路徑走

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 1.創建
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anim.duration = 2;
    // 2.設置屬性
    anim.path = [UIBezierPath bezierPathWithOvalInRect:self.view.bounds].CGPath;

    // 3.添加
    [self.magLayer addAnimation:anim forKey:nil];
}

圖標抖動效果

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 1.創建關鍵幀動畫
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

    // 2.設置屬性
    anim.values = @[@(-M_PI_4 * 0.2),@(M_PI_4 * 0.2), @(-M_PI_4 * 0.2)];
    anim.repeatCount = CGFLOAT_MAX;

    // 3.添加
    [self.magLayer addAnimation:anim forKey:nil];
}

改變大小

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 修改大小
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"bounds.size"];

    anim.values = @[
                    [NSValue valueWithCGSize:CGSizeMake(200, 200)],
                    [NSValue valueWithCGSize:CGSizeMake(80, 80)],
                    ];

    anim.repeatCount = CGFLOAT_MAX;

    [self.magLayer addAnimation:anim forKey:nil];
}

3、CAAnimationGroup(動畫組)

動畫組,是CAAnimation的子類,可以保存一組動畫對象,將CAAnimationGroup對象加入層后,組中所有動畫對象可以同時并發運行。
默認情況下,一組動畫對象是同時運行的,也可以通過設置動畫對象的beginTime屬性來更改動畫的開始時間。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 1.創建組動畫對象
    CAAnimationGroup *group = [CAAnimationGroup animation];

    // 2.設置動畫屬性
    group.animations = @[
                         [self anim1],
                         [self anim2],
                         [self anim3],
                         ];
    group.duration = 2;
    group.repeatCount = CGFLOAT_MAX;

    // 3.添加到layer上
    [self.magLayer addAnimation:group forKey:nil];
}

4、CATransition(轉場動畫)

CATransition是CAAnimation的子類,用于做轉場動畫,能夠為層提供移出屏幕和移入屏幕的動畫效果。iOS比Mac OS X的轉場動畫效果少一點。
UINavigationController就是通過CATransition實現了將控制器的視圖推入屏幕的動畫效果。

#pragma mark - 2.輕掃觸發
- (void)swipeAction:(UISwipeGestureRecognizer *)recognizer {

    // MARK: - 轉場動畫
    // 1.創建動畫對象
    CATransition *anim = [CATransition animation];

    // 兩個屬性 type[動畫的類型], subType[子類型]

    // 2.設置動畫屬性
    anim.type = kCATransitionReveal;

    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {

        NSLog(@"向右");
        anim.subtype = kCATransitionFromLeft;

    } else {

        NSLog(@"向左");
        anim.subtype = kCATransitionFromRight;
    }

    UIImageView *imgView = (UIImageView *)recognizer.view;
    imgView.image = [UIImage imageNamed:imgName];

    // 3.添加動畫
    [imgView.layer addAnimation:anim forKey:nil];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容