Core Animationを理解する -Basic Animation-

Basic AnimationnはCoreAnimationClassに含まれている
以下のような動きをサポート
・x,y軸への一定速度の移動
・x,yを基軸とした回転
・拡大、縮小の設定
・移動時間のタイミング


Basic Animationnを理解するために
layerClassを作成して画像が動作するプログラムを作成する

手順としては

1.QuartzCore.frameworkを入れる
2.それ用のLayerクラスを作成する
3.ViewControllerにてLayerクラスを呼び出し
4.animationを追加したげる

1.QuartzCore.frameworkを入れる(省略)

2.それ用のLayerクラスを作成する

・ここではBasicAnimationlayerクラスを作成,追加したQuartzCoreをimprotしてLayerの基本情報を記述する

3.ViewControllerにてLayerクラスを呼び出し

BasicAnimationViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Basic Animation";

    // レイヤーの呼び出し
    if (!basicAnimationlayer) {
        basicAnimationlayer = [[BasicAnimationLayer alloc] init];
        basicAnimationlayer.frame = CGRectMake(10, 100, 120, 119);
        
        [self.view.layer addSublayer:basicAnimationlayer];
        //動作を開始
        [self startAnimation];
    }
}

4.animationを追加したげる

-(void)startAnimation:(id)sender
{
    // 縦横の移動範囲を設定
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [NSValue valueWithCGPoint:basicAnimationlayer.position];
    CGPoint toPoint = basicAnimationlayer.position;
    toPoint.x += 580;
    animation.toValue = [NSValue valueWithCGPoint:toPoint];
    
    // 回転方向を設定
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    rotateAnimation.toValue = [NSNumber numberWithFloat:1.0 * M_PI];
    
    // 拡大縮小を設定
    CABasicAnimation *scaoleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaoleAnimation.duration = 0.5;
    scaoleAnimation.autoreverses = YES;
    scaoleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaoleAnimation.toValue = [NSNumber numberWithFloat:3.5];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.autoreverses = YES;
    group.duration = 1.0;
    group.animations = [NSArray arrayWithObjects:animation, scaoleAnimation, rotateAnimation, nil];
    group.repeatCount = NSNotFound;
    
    [basicAnimationlayer addAnimation:group forKey:@"move"];
}