Objective-Cでフェードイン・フェードアウトアニメーションを実行するサンプルを以下に作成しました。ボタンを押すと背景画像がアニメーションします。
サンプルダウンロード
ソースコード
ViewController.h
1
2
3
4
5
6
7
8
9
10
11
12
| #import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
BOOL _isFadeIn;
}
@property (weak, nonatomic) IBOutlet UIButton *sampleButton;
@property (weak, nonatomic) IBOutlet UIImageView *sampleImageView;
- (IBAction)buttonFadeInOut:(id)sender;
@end
|
ViewController.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
55
56
57
58
59
60
61
62
63
64
65
| #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_isFadeIn = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)buttonFadeInOut:(id)sender {
if (_isFadeIn) {
//フェードアウトアニメーション実行
[self sampleImageFadeOut];
//ボタンのテキストを変更
[_sampleButton setTitle:@"fadeIn" forState:UIControlStateNormal];
} else {
//フェードインアニメーション実行
[self sampleImageFadeIn];
//ボタンのテキストを変更
[_sampleButton setTitle:@"fadeOut" forState:UIControlStateNormal];
}
_isFadeIn = !_isFadeIn;
}
- (void)sampleImageFadeIn
{
//フェードイン
_sampleImageView.alpha = 0;
//アニメーションのタイプを指定
[UIView beginAnimations:@"fadeIn" context:nil];
//イージング指定
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//アニメーション秒数を指定
[UIView setAnimationDuration:0.3];
//目標のアルファ値を指定
_sampleImageView.alpha = 1;
//アニメーション実行
[UIView commitAnimations];
}
- (void)sampleImageFadeOut
{
//フェードアウト
[UIView beginAnimations:@"fadeOut" context:nil];
//イージング指定
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//アニメーション秒数を指定
[UIView setAnimationDuration:0.15];
//目標のアルファ値を指定
_sampleImageView.alpha = 0;
//アニメーション実行
[UIView commitAnimations];
}
@end
|
関連記事
お薦めの参考書
Cocos2d-xでゲームを開発したいという方に必携の1冊です。実際にゲームを作りながら学習して行く内容で、とても身に付き易い構成になっています。プログラミング経験はあるけれどゲーム開発は未経験という方にお薦めの内容となっております。