落書き的な、福笑いアプリの作り方メモ
(Xcode iOS6 Simulatorで試しています。)
ポイント
・ルーレットでパーツの縦横の座標を決定する
・UIView.tagでルーレットの点滅を管理する
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
// ルーレットの状態
typedef enum {
RouletteStop,
RouletteVertical,
RouletteHorizontal,
} Status;
@interface ViewController () {
NSTimer *timer;
Status status; // ルーレットのステータス
int next; // ルーレットで光らせるviewのタグをいれとく
}
@property (nonatomic, strong) UIView *roulette;
@end
@implementation ViewController
@synthesize roulette;
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
– (void)viewDidAppear:(BOOL)animated
{
// 初期化処理
[self.view addSubview:[self createRoulette]];
status = RouletteStop;
// 大きくへのへのもへじの「じ」の文字を表示
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @”じ“;
label.font = [UIFont systemFontOfSize:400];
[label sizeToFit];
label.center = self.view.center;
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
// タイマースタート
[self start];
}
– (void)viewDidDisappear:(BOOL)animated
{
[timer invalidate];
timer = nil;
}
– (UIView*)createRoulette
{
// ルーレットViewの作成
roulette = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 400)];
float size = 20;
float x, y;
for (int i=0; i<10; i++) {
// 縦
x = 5;
y = 40 * i + 5;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x, y, size, size)];
v.backgroundColor = [UIColor redColor];
v.layer.borderColor = [UIColor whiteColor].CGColor;
v.layer.borderWidth = 2.0;
v.alpha = 0.5;
v.tag = i + 100;
[roulette addSubview:v];
}
for (int i=0; i<10; i++) {
// 横
x = 30 * i + 5;
y = 5;
UIView *h = [[UIView alloc] initWithFrame:CGRectMake(x, y, size, size)];
h.backgroundColor = [UIColor greenColor];
h.layer.borderColor = [UIColor whiteColor].CGColor;
h.layer.borderWidth = 2.0;
h.tag = i + 200;
h.alpha = 0.5;
[roulette addSubview:h];
}
return roulette;
}
– (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(roulette:) userInfo:nil repeats:YES];
[timer fire];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// タッチで止める
// 一回目は、縦
// 二回目は、横
// 横が止まったときに、順番に文字を表示する。
static NSMutableArray *faceParts;
if (!faceParts) {
faceParts = [NSMutableArray arrayWithObjects:@”へ“,@”の“,@”へ“,@”の“,@”も“,@”へ“,nil];
}
if (status == RouletteStop) {
status = RouletteVertical;
}
else if (status == RouletteVertical) {
status = RouletteHorizontal;
}
else if (status == RouletteHorizontal) {
status = RouletteStop;
UILabel *label = [[UILabel alloc] init];
float y = [roulette viewWithTag:999].center.y;
float x = [roulette viewWithTag:9999].center.x;
label.center = CGPointMake(x, y);
label.font = [UIFont boldSystemFontOfSize:40.0];
label.text = [faceParts objectAtIndex:0];
[faceParts removeObjectAtIndex:0];
label.backgroundColor = [UIColor clearColor];
[label sizeToFit];
[self.view addSubview:label];
}
}
– (void)roulette:(NSTimer*)sender
{
// ルーレットをまわす。
// Viewのタグでどこが光っているか管理する
if(status == RouletteVertical) {
for (int i = 0; i< [roulette.subviews count]; i++) {
UIView *v = [roulette.subviews objectAtIndex:i];
if (v.tag == 999) {
v.alpha = 0.5;
v.tag = 100 + i;
next = 100 + (i + 1) % 10;
}
}
if (next == 0) {
next = 100;
}
UIView *v = [roulette viewWithTag:next];
v.tag = 999;
v.alpha = 1.0;
} else if (status == RouletteHorizontal) {
for (int i = 0; i< [roulette.subviews count]; i++) {
UIView *v = [roulette.subviews objectAtIndex:i];
if (v.tag == 9999) {
v.alpha = 0.5;
v.tag = 200 + i – 10;
next = 200 + (i –10 + 1) % 10;
}
}
if (next < 200) {
next = 200;
}
UIView *v = [roulette viewWithTag:next];
v.tag = 9999;
v.alpha = 1.0;
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end