ABCDEの5文字が書いてある板を4枚4色で用意して、
その板を、蒸気船のパドルのようにクルクル回すサンプルです。
画面上に、スピードアップ、ダウン、時計回り、反時計回り
のボタンを用意して、動きを組み合わせられるようにしてみます。
ポイント
フォントにboldSystemFontを利用して、
UILabelのanchorPointをCGPointMake(0.5, 0.8)とすることで、
ちょうど、文字の下端でくっついているように見せています。
環境
今回つくったiPhoneゲームのサンプルコードは、
XcodeのiOS6 iPhone Simulatorで動かしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSTimer *timer;
UIView *base;
float velocity;
BOOL turnCW;
BOOL turnCCW;
BOOL speedUP;
BOOL speedDown;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createWords];
velocity = M_PI * 0.01;
[self createUI];
[self startTimer];
}
– (void)createWords
{
base = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
base.center = CGPointMake(160, 150);
base.backgroundColor = [UIColor clearColor];
[self.view addSubview:base];
NSArray *words = [@”A B C D E” componentsSeparatedByString:@” “];
NSArray *colors = [NSArray arrayWithObjects:[UIColor whiteColor], [UIColor greenColor], [UIColor yellowColor], [UIColor purpleColor], nil];
for (int i=0; i<[words count]; i++) {
for (int j=0; j<4; j++) {
UILabel *hole = [[UILabel alloc] init];
hole.center = CGPointMake(i * 50 + 50, 0);
hole.backgroundColor = [UIColor clearColor];
hole.textColor = [colors objectAtIndex:j];
hole.text = [words objectAtIndex:i];
hole.font = [UIFont boldSystemFontOfSize:40];
hole.textAlignment = 1;
hole.layer.anchorPoint = CGPointMake(0.5, 0.8);
hole.tag = 1;
[hole sizeToFit];
[base addSubview:hole];
hole.layer.transform = CATransform3DMakeRotation(M_PI * 0.5 * j, 1, 0, 0);
}
}
}
– (void)createUI
{
UIView *btnA = [[UIView alloc] initWithFrame:CGRectMake(50, 400, 40, 40)];
btnA.backgroundColor = [UIColor redColor];
btnA.layer.cornerRadius = 5.0;
btnA.tag = 11;
[self.view addSubview:btnA];
UIView *btnB = [[UIView alloc] initWithFrame:CGRectMake(100, 400, 40, 40)];
btnB.backgroundColor = [UIColor orangeColor];
btnB.layer.cornerRadius = 5.0;
btnB.tag = 12;
[self.view addSubview:btnB];
UIView *btnC = [[UIView alloc] initWithFrame:CGRectMake(180, 400, 40, 40)];
btnC.backgroundColor = [UIColor cyanColor];
btnC.layer.cornerRadius = 5.0;
btnC.tag = 13;
[self.view addSubview:btnC];
UIView *btnD = [[UIView alloc] initWithFrame:CGRectMake(230, 400, 40, 40)];
btnD.backgroundColor = [UIColor blueColor];
btnD.layer.cornerRadius = 5.0;
btnD.tag = 14;
[self.view addSubview:btnD];
NSArray *btns = [NSArray arrayWithObjects:btnA, btnB, btnC, btnD, nil];
for (UIView *btn in btns) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[btn addGestureRecognizer:tap];
}
}
– (void)tap:(UITapGestureRecognizer*)gr
{
turnCW = NO;
turnCCW = NO;
speedUP = NO;
speedDown = NO;
if (gr.view.tag == 11) {
turnCCW = YES;
} else if (gr.view.tag == 12) {
turnCW = YES;
} else if (gr.view.tag == 13) {
speedUP = YES;
} else if (gr.view.tag == 14) {
speedDown = YES;
}
}
– (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
if (turnCW) {
base.transform = CGAffineTransformRotate(base.transform, M_PI * 0.01);
} else if (turnCCW) {
base.transform = CGAffineTransformRotate(base.transform, –M_PI * 0.01);
} else if (speedUP) {
velocity = MIN(velocity + M_PI * 0.0001, M_PI * 0.05);
} else if (speedDown) {
velocity = MAX(velocity – M_PI * 0.0001, 0);
}
for (UIView *v in base.subviews) {
v.layer.transform = CATransform3DRotate(v.layer.transform, velocity, 1, 0, 0);
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end