5種類のアニメーションをシンプルな記述で実行する「DCAnimationLite」クラスを作成しました。主な機能は以下の通りです。
主な機能
フェードイン・フェードアウトアニメーション
スライドインアニメーション
回転アニメーション
拡大縮小アニメーション
平行移動アニメーション
ダウンロード
サンプルソースコード
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#import "ViewController.h"
@interface ViewController ()
@property BOOL isAnimationTargetFadeIn ;
@end
@implementation ViewController
- ( void ) viewDidLoad
{
[ super viewDidLoad ];
}
//フェードアニメーション実行
- ( IBAction ) startFadeAnimation: ( id ) sender {
//アニメーション秒数とフェードモードを指定
[ DCAnimation fade: self . animationTargetImage
duration: 0.5f
isFadeIn: _isAnimationTargetFadeIn ];
//フェードインフラグ切り替え
_isAnimationTargetFadeIn = ! _isAnimationTargetFadeIn ;
}
//スライドアニメーション実行
- ( IBAction ) startSlideAnimation: ( id ) sender {
//アニメーション秒数と目標座標・サイズを指定
[ DCAnimation slide: self . animationTargetImage
duration: 0.5f
aimRect: CGRectMake ( 64 , 64 , 57 , 57 )];
}
//回転アニメーション実行
- ( IBAction ) startRotateAnimation: ( id ) sender {
//アニメーション秒数と目標回転度数を指定
[ DCAnimation rotate: self . animationTargetImage
duration: 0.5f
aimAngle: 90 ];
}
- ( IBAction ) startScaleAnimation: ( id ) sender {
//アニメーション秒数と目標スケール値を指定
[ DCAnimation scale: self . animationTargetImage
duration: 0.5f
aimScale: 2.0f ];
}
- ( IBAction ) startTranslateAnimation: ( id ) sender {
//アニメーション秒数とXYの移動量を指定
[ DCAnimation translate: self . animationTargetImage
duration: 0.5f
movePosition: 100 ];
}
@end
ソースコード
DCAnimationLite.h
1
2
3
4
5
6
7
8
9
10
11
12
#import <UIKit/UIKit.h>
@interface DCAnimationLite : UIView
#pragma mark - public method
+ ( void ) fade: ( UIView * ) imageView duration: ( float ) duration isFadeIn: ( BOOL ) isFadeIn ;
+ ( void ) slide: ( UIView * ) imageView duration: ( float ) duration aimRect: ( CGRect ) rect ;
+ ( void ) rotate: ( UIView * ) imageView duration: ( float ) duration aimAngle: ( float ) angle ;
+ ( void ) scale: ( UIView * ) imageView duration: ( float ) duration aimScale: ( float ) scale ;
+ ( void ) translate: ( UIView * ) imageView duration: ( float ) duration movePosition: ( float ) position ;
@end
DCAnimationLite.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#import "DCAnimationLite.h"
@implementation DCAnimationLite
// フェードアニメーション
+ ( void ) fade: ( UIView * ) imageView duration: ( float ) duration isFadeIn: ( BOOL ) isFadeIn
{
[ UIView beginAnimations: nil context: NULL ];
[ UIView setAnimationDuration: duration ];
imageView . alpha = isFadeIn ? 0 : 1.0f ;
imageView . alpha = isFadeIn ? 1.0f : 0 ;
[ UIView commitAnimations ];
}
// スライドアニメーション
+ ( void ) slide: ( UIView * ) imageView duration: ( float ) duration aimRect: ( CGRect ) rect
{
[ UIView beginAnimations: nil context: NULL ];
[ UIView setAnimationDuration: duration ];
[ imageView setFrame: rect ];
[ UIView commitAnimations ];
}
// 回転アニメーション
+ ( void ) rotate: ( UIView * ) imageView duration: ( float ) duration aimAngle: ( float ) angle
{
[ UIView beginAnimations: nil context: NULL ];
[ UIView setAnimationDuration: duration ];
CGAffineTransform rotate = CGAffineTransformMakeRotation ( angle * ( M_PI / 180.0f ));
[ imageView setTransform: rotate ];
[ UIView commitAnimations ];
}
// 拡縮アニメーション
+ ( void ) scale: ( UIView * ) imageView duration: ( float ) duration aimScale: ( float ) scale
{
[ UIView beginAnimations: nil context: NULL ];
[ UIView setAnimationDuration: duration ];
CGAffineTransform aimScale = CGAffineTransformMakeScale ( scale , scale );
[ imageView setTransform: aimScale ];
[ UIView commitAnimations ];
}
// XY方向に平行移動
+ ( void ) translate: ( UIView * ) imageView duration: ( float ) duration movePosition: ( float ) position
{
[ UIView beginAnimations: nil context: NULL ];
[ UIView setAnimationDuration: duration ];
CGAffineTransform translate = CGAffineTransformMakeTranslation ( position , position );
[ imageView setTransform: translate ];
[ UIView commitAnimations ];
}
@end
お薦めの参考書
Cocos2d-xでゲームを開発したいという方に必携の1冊です。実際にゲームを作りながら学習して行く内容で、とても身に付き易い構成になっています。プログラミング経験はあるけれどゲーム開発は未経験という方にお薦めの内容となっております。