[ios]CA animationWIthKeyPath的一些思考

使用CABasicAnimation的时候 总会遇到 animationWithKeyPath的情况 keyPath到底是什么呢?

今天查了下文档,大概意思是说使用此动画的对象 的key (也就是这个使用动画的对象能响应个消息)。

其中尝试对backgroundColor进行设置

旋转于放大缩小使用transform

补充1:

keypath 是 layer属性 注释说明中含有Animatable的

资料:http://objccn.io/issue-12-4/

资料:http://blog.csdn.net/lvxiangan/article/details/17167827

//
//  PLLViewController.m
//  LNCAnimation
//
//  Created by liu poolo on 14-5-13.
//  Copyright (c) 2014年 liu poolo. All rights reserved.
//

#import "PLLViewController.h"
#import <QuartzCore/QuartzCore.h>



@interface PLLViewController ()

@end

@implementation PLLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    CALayer *_cl=[[CALayer alloc] init];
    _cl.backgroundColor=[[UIColor grayColor] CGColor];
    _cl.frame=CGRectMake(10, 10, 40, 40);
    _cl.cornerRadius=5.0f;
    
    
    CABasicAnimation *_ca=[CABasicAnimation animationWithKeyPath:@"position"];//对哪个通道进行动画描写
    _ca.fromValue=[NSValue valueWithCGPoint:_cl.position];
    CGPoint toPoint=_cl.position;
    toPoint.x+=100;
    _ca.toValue=[NSValue valueWithCGPoint:toPoint];
    [self.view.layer addSublayer:_cl];
    _ca.autoreverses=YES;
    _ca.duration=1.0f;
    _ca.repeatCount=NSNotFound;

//    [_cl addAnimation:_ca forKey:@"kLayerPLMove"];
    
    
    CABasicAnimation *_caRotato = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    _caRotato.fromValue = [NSNumber numberWithFloat:0.0f];
    _caRotato.toValue = [NSNumber numberWithFloat:6.0f*M_PI];
    _caRotato.autoreverses=YES;
    _caRotato.duration=10.0f;
    _caRotato.repeatCount=NSNotFound;
    
    CABasicAnimation *_caScaole = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    _caScaole.fromValue = [NSNumber numberWithFloat:1.0f];
    _caScaole.toValue = [NSNumber numberWithFloat:3.0f];
    _caScaole.autoreverses=YES;
    _caScaole.duration=10.0f;
    _caScaole.repeatCount=NSNotFound;
    
    CABasicAnimation *_caColor=[CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    //animationWithKeyPath后跟随的是CALayer可接受的Key即可 既使用动画的对象的Key
    _caColor.fromValue=(__bridge id)([[UIColor grayColor] CGColor]);
    _caColor.toValue=(__bridge id)([[UIColor redColor] CGColor]);
    
    CAAnimationGroup *_caGroup=[CAAnimationGroup animation];
    [_caGroup setAnimations:[NSArray arrayWithObjects:_ca,_caRotato,_caScaole,_caColor,nil]];
    _caGroup.duration=10.0f;
    _caGroup.repeatCount=NSNotFound;
    _caGroup.autoreverses=YES;
    
    
    [_cl addAnimation:_caGroup forKey:@"GroupKey"];

//    [_cl addAnimation:_caRotato forKey:@"rotatoKey"];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

相关推荐