▼iPhoneプログラミングメモ▼
レイヤーアニメーション

レイヤーアニメーションを行うプログラムを作成する。



画像の準備
Resourcesに追加。

heniheni.png


ソースコードの記述
LayerAnimationEx.h
#import <UIKit/UIKit.h>
#import <QuartzCore/CAAnimation.h>
#import <QuartzCore/CALayer.h>

//LayerAnimationExの宣言
@interface LayerAnimationEx : UIViewController {
}
@end

LayerAnimationEx.m
#import "LayerAnimationEx.h"

//LayerAnimationExの実装
@implementation LayerAnimationEx

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];

    //イメージの読み込み
    UIImage* image=[UIImage imageNamed:@"sample.png"];

    //レイヤーの生成
    CALayer* layer=[CALayer layer];
    layer.frame   =CGRectMake(0,0,image.size.width,image.size.height);
    layer.position=CGPointMake(160,160);
    layer.contents=(UIView*)image.CGImage;
    
    //ビューへのレイヤーの追加
    [self.view.layer addSublayer:layer];
        
    //レイヤーアニメーションの生成
    CABasicAnimation* anime=[CABasicAnimation 
        animationWithKeyPath:@"transform"];
    anime.duration=0.5f;
    anime.repeatCount=999;
    anime.autoreverses=YES;
    anime.fromValue=[NSValue valueWithCATransform3D:
        CATransform3DIdentity];
    anime.toValue=[NSValue valueWithCATransform3D:
        CATransform3DMakeRotation(M_PI,0,1,0)];
        
    //レイヤーへのレイヤーアニメーションの追加
    [layer addAnimation:anime forKey:@"rotation"];
}

//メモリ解放
- (void)dealloc {
    [super dealloc];
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end


−戻る−