IOSbookSeminar1 » 履歴 » バージョン 3
Yuumi Yoshida, 2015-08-03 22:24
1 | 3 | Yuumi Yoshida | # ハンズオン資料 |
---|---|---|---|
2 | 1 | Yuumi Yoshida | |
3 | 3 | Yuumi Yoshida | ### BMIアプリのコード |
4 | 1 | Yuumi Yoshida | |
5 | 2 | Yuumi Yoshida | * リスト02-01 |
6 | |||
7 | 3 | Yuumi Yoshida | ~~~ |
8 | 2 | Yuumi Yoshida | // |
9 | // ViewController.m |
||
10 | // |
||
11 | // |
||
12 | |||
13 | #import "ViewController.h" |
||
14 | |||
15 | @interface ViewController () |
||
16 | @property (weak, nonatomic) IBOutlet UITextField *weightText; |
||
17 | @property (weak, nonatomic) IBOutlet UITextField *heightText; |
||
18 | @property (weak, nonatomic) IBOutlet UILabel *resultLabel; |
||
19 | @end |
||
20 | |||
21 | @implementation ViewController |
||
22 | |||
23 | - (void)viewDidLoad |
||
24 | { |
||
25 | [super viewDidLoad]; |
||
26 | } |
||
27 | |||
28 | - (void)didReceiveMemoryWarning |
||
29 | { |
||
30 | [super didReceiveMemoryWarning]; |
||
31 | } |
||
32 | |||
33 | - (IBAction)bmiButtonPushed:(id)sender |
||
34 | { |
||
35 | float h = _heightText.text.floatValue / 100; |
||
36 | float w = _weightText.text.floatValue; |
||
37 | |||
38 | float bmi = w / (h * h); |
||
39 | NSLog(@"bmi = %f", bmi); |
||
40 | |||
41 | if (bmi >= 25) { |
||
42 | _resultLabel.text = @"太りすぎ"; |
||
43 | } else if (bmi < 18.5) { |
||
44 | _resultLabel.text = @"やせすぎ"; |
||
45 | } else { |
||
46 | _resultLabel.text = @"標準"; |
||
47 | } |
||
48 | } |
||
49 | @end |
||
50 | 3 | Yuumi Yoshida | ~~~ |
51 | 1 | Yuumi Yoshida | |
52 | 2 | Yuumi Yoshida | * リスト02-02 |
53 | |||
54 | 3 | Yuumi Yoshida | ~~~ |
55 | 2 | Yuumi Yoshida | if (bmi >= 25) { |
56 | _resultLabel.text = @"太りすぎ"; |
||
57 | _resultLabel.backgroundColor = [UIColor redColor]; |
||
58 | } else if (bmi < 18.5) { |
||
59 | _resultLabel.text = @"やせすぎ"; |
||
60 | _resultLabel.backgroundColor = [UIColor yellowColor]; |
||
61 | } else { |
||
62 | _resultLabel.text = @"標準"; |
||
63 | _resultLabel.backgroundColor = [UIColor greenColor]; |
||
64 | } |
||
65 | 3 | Yuumi Yoshida | ~~~ |
66 | 2 | Yuumi Yoshida | |
67 | * リスト02-03 |
||
68 | |||
69 | 3 | Yuumi Yoshida | ~~~ |
70 | 2 | Yuumi Yoshida | #import <AVFoundation/AVFoundation.h> |
71 | 3 | Yuumi Yoshida | ~~~ |
72 | 2 | Yuumi Yoshida | |
73 | |||
74 | * リスト02-04 |
||
75 | |||
76 | 3 | Yuumi Yoshida | ~~~ |
77 | 2 | Yuumi Yoshida | @property (strong, nonatomic) AVAudioPlayer *audioPlayer; |
78 | 3 | Yuumi Yoshida | ~~~ |
79 | 2 | Yuumi Yoshida | |
80 | |||
81 | * リスト02-05 |
||
82 | |||
83 | 3 | Yuumi Yoshida | ~~~ |
84 | 2 | Yuumi Yoshida | - (void) sound:(NSString *)file |
85 | { |
||
86 | NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] |
||
87 | pathForResource:file ofType:@"aif"]]; |
||
88 | _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; |
||
89 | [_audioPlayer play]; |
||
90 | } |
||
91 | 3 | Yuumi Yoshida | ~~~ |
92 | 2 | Yuumi Yoshida | |
93 | |||
94 | * リスト02-06 |
||
95 | |||
96 | 3 | Yuumi Yoshida | ~~~ |
97 | 2 | Yuumi Yoshida | if (bmi >= 25) { |
98 | _resultLabel.text = @"太りすぎ"; |
||
99 | _resultLabel.backgroundColor = [UIColor redColor]; |
||
100 | [self sound:@"fat"]; |
||
101 | } else if (bmi < 18.5) { |
||
102 | _resultLabel.text = @"やせすぎ"; |
||
103 | _resultLabel.backgroundColor = [UIColor yellowColor]; |
||
104 | [self sound:@"skinny"]; |
||
105 | } else { |
||
106 | _resultLabel.text = @"標準"; |
||
107 | _resultLabel.backgroundColor = [UIColor greenColor]; |
||
108 | [self sound:@"normal"]; |
||
109 | } |
||
110 | 3 | Yuumi Yoshida | ~~~ |
111 | 2 | Yuumi Yoshida | |
112 | 3 | Yuumi Yoshida | ### サウンドファイル |