アルファベットABC…をクルクル螺旋状にまわしてみる。

(XcodeのiOS6 iPhone Simulatorで動かしています。)

ポイント

・文字のカラーは colorWithHue の値をインクリメントしてカラフルに

・タッチ中はそのポイント方向に文字を動かすようにする。

サンプルコード

#import “ViewController.h”

@interface ViewController () {

    NSTimer *timer;

    int direction;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self start];

    direction = –1;

}

– (void)createWord

{

    static int number;

    

    NSArray *words = [@”A B C D E F G H I J K L M N O P Q R S T U V W X Y Z” componentsSeparatedByString:@” “];

    

    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    l.font = [UIFont systemFontOfSize:30];

    l.text = [words objectAtIndex:number];

    l.textColor = [UIColor colorWithHue:number/30.0 saturation:1.0 brightness:1.0 alpha:1.0];

    l.backgroundColor = [UIColor clearColor];

    l.center = self.view.center;

    [self.view addSubview:l];

    l.tag = number;

    number++;

    number = number % [words count];

}

– (void)start

{

    timer = [NSTimer scheduledTimerWithTimeInterval:2.0/60.0 target:self selector:@selector(updateDisp:) userInfo:nil repeats:YES];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    float angle = atan2(p.xself.view.center.x, -p.y + self.view.center.y) + M_PI * 1.5;

    direction = 26 * (angle / (M_PI * 2.0));

}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    float angle = atan2(p.xself.view.center.x, -p.y + self.view.center.y) + M_PI * 1.5;

    direction = 26 * (angle / (M_PI * 2.0));

}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    direction = – 1;

}

– (void)updateDisp:(NSTimer*)sender

{

    [self createWord];

    

    if ([self.view.subviews count] > 100) {

        [[self.view.subviews objectAtIndex:0] removeFromSuperview];

    }

    

    for (UIView *v in self.view.subviews) {

        

        

        float angle = (M_PI/13.0) * v.tag;

        if (direction < 0) {

        } else {

            angle = (M_PI/13.0) * direction;

        }

        float x = 5 * cos(angle);

        float y = 5 * sin(angle);

        v.transform = CGAffineTransformTranslate(v.transform, x, y);

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

@end