3分でできるプログラミング
#import “ViewController.h”
@interfaceViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor = [UIColorgreenColor];
// road
// horizontal
for (int i=0; i<3; i++) {
double x = 0;
double y = 120 * i + 35;
double width = self.view.frame.size.width;
double height = 80;
UIView *road = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
road.backgroundColor = [UIColordarkGrayColor];
// center line
for (int j=0; j<5; j++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 5)];
line.center = CGPointMake(j * 70, road.bounds.size.height / 2.0);
line.backgroundColor = [UIColor whiteColor];
[road addSubview:line];
[self.view addSubview:road];
}
}
// 車を並べる
[selflineupCars];
// 発車ボタン
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0,0, 50, 20);
btn.center = CGPointMake(self.view.center.x, 15);
[btn setTitle:@”Go!”forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(go) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
– (void)lineupCars
{
double carCenterY = self.view.bounds.size.height – 60;
// clean old lineup
for (UIView *v inself.view.subviews) {
if (v.center.y == carCenterY) {
[v removeFromSuperview];
}
}
// 注意: mac じゃないと文字コードで化けるかも。
// icons には使いたい絵文字を , 区切りで入れてください。
NSArray *icons = [@”