iPhoneコーヒーカップ

遊園地のコーヒーカップみたいな動きをするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UIView *coffeeTable;

@property (nonatomic, weak) NSTimer *timer;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1];

    [self createCoffeeTable];

}

#define Radius MIN(CGRectGetMaxX(self.view.bounds), CGRectGetMaxY(self.view.bounds))

– (void)createCoffeeTable

{

    float r = Radius;

    UIView *coffeeTable = [[UIView alloc] initWithFrame:CGRectMake(0, 0, r, r)];

    coffeeTable.backgroundColor = [UIColor colorWithHue:0.3 saturation:0.9 brightness:1 alpha:1];

    coffeeTable.layer.cornerRadius = r * 0.5;

    coffeeTable.center = CGPointMake(160, 220);

    [self.view addSubview:coffeeTable];

    

    self.coffeeTable = coffeeTable;

    

    for (int i=0; i<3; i++) {

        float x = r * 0.26 * cos(i * M_PI * (2.0/3.0)) + r * 0.5;

        float y = r * 0.26 * sin(i * M_PI * (2.0/3.0)) + r * 0.5;

        [self createCoffePlateAtPoint:CGPointMake(x, y)];

    }

}

– (void)createCoffePlateAtPoint:(CGPoint)p

{

    float r = 0.43 * Radius;

    UIView *coffeePlate = [[UIView alloc] initWithFrame:CGRectMake(0, 0, r, r)];

    coffeePlate.backgroundColor = [UIColor colorWithHue:0.3 saturation:0.4 brightness:0.8 alpha:1];

    coffeePlate.layer.cornerRadius = r * 0.5;

    coffeePlate.center = p;

    [self.coffeeTable addSubview:coffeePlate];

    

    for (int i=0; i<3; i++) {

        float x = r * 0.26 * cos(i * M_PI * (2.0/3.0)) + r * 0.5;

        float y = r * 0.26 * sin(i * M_PI * (2.0/3.0)) + r * 0.5;

        [self createCupOn:coffeePlate AtPoint:CGPointMake(x, y) angle:i * M_PI * 2.0/3.0];

    }

}

– (UIView *)createCupOn:(UIView *)plate AtPoint:(CGPoint)p angle:(float)angle

{

    float r = 0.4 * 0.4 * Radius;

    UIView *coffeeCup = [[UIView alloc] initWithFrame:CGRectMake(0, 0, r, r)];

    coffeeCup.backgroundColor = [UIColor whiteColor];

    coffeeCup.layer.cornerRadius = r * 0.5;

    coffeeCup.center = p;

    [plate addSubview:coffeeCup];

    

    CALayer *handle = [CALayer layer];

    handle.frame = CGRectMake(r*0.4, 4, 8, –12);

    handle.backgroundColor = [UIColor whiteColor].CGColor;

    handle.cornerRadius = 4;

    [coffeeCup.layer addSublayer:handle];

    

    CALayer *coffee = [CALayer layer];

    coffee.frame = CGRectInset(coffeeCup.bounds, 4, 4);

    coffee.cornerRadius = (r – 8) * 0.5;

    coffee.backgroundColor = [UIColor brownColor].CGColor;

    [coffeeCup.layer addSublayer:coffee];

    

    coffeeCup.transform = CGAffineTransformMakeRotation(angle);

    return coffeeCup;

}

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

{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(turn) userInfo:nil repeats:YES];

}

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

{

    [self.timer invalidate];

    self.timer = nil;

}

– (void)turn

{

    [self.view.subviews enumerateObjectsUsingBlock:^(UIView *table, NSUInteger idx, BOOL *stop) {

        table.transform = CGAffineTransformRotate(table.transform, 0.02);

        [table.subviews enumerateObjectsUsingBlock:^(UIView *plate, NSUInteger idx, BOOL *stop) {

            plate.transform = CGAffineTransformRotate(plate.transform, –0.05);

            [plate.subviews enumerateObjectsUsingBlock:^(UIView *cup, NSUInteger idx, BOOL *stop) {

                cup.transform = CGAffineTransformRotate(cup.transform, 0.1);

            }];

        }];

    }];

}

@end