iPhoneろくろ盤

まわっている円盤に模様を付けていくiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) CAShapeLayer *turnTable;

@property (nonatomic, strong) NSValue *previousPoint;

@property (nonatomic, strong) NSValue *touchPoint;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [self color:0];

    [self createTurnTable];

    [self createTitle];

    

    [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(printPoint) userInfo:nil repeats:YES];

}

– (void)createTitle

{

    UILabel *title = [[UILabel alloc] init];

    title.text = @”pottery wheels”;

    title.font = [UIFont fontWithName:@”Chalkduster” size:30];

    title.textColor = [self color:1];

    [title sizeToFit];

    title.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) – 80);

    [self.view addSubview:title];

}

– (void)createTurnTable

{

    UIBezierPath *path = [self defaultPath];

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.path = path.CGPath;

    layer.fillColor = [self color:2].CGColor;

    layer.strokeColor = [self color:0].CGColor;

    layer.lineWidth = 5;

    layer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) – 50);

    [self.view.layer addSublayer:layer];

    self.turnTable = layer;

    

    CABasicAnimation *turn = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”];

    turn.duration = 8.0;

    turn.repeatCount = HUGE_VAL;

    turn.fromValue = @0;

    turn.toValue = @(2.0 * M_PI);

    [self.turnTable addAnimation:turn forKey:nil];

}

– (UIBezierPath *)defaultPath

{

    float r = 150;

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:r startAngle:0 endAngle:2.0 * M_PI clockwise:YES];

    [path moveToPoint:CGPointMake(0, -r)];

    [path addLineToPoint:CGPointMake(0, r)];

    return path;

}

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

{

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

    self.touchPoint = [NSValue valueWithCGPoint:p];

}

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

{

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

    self.touchPoint = [NSValue valueWithCGPoint:p];

}

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

{

    self.touchPoint = nil;

    self.previousPoint = nil;

}

– (void)printPoint

{

    if (self.touchPoint) {

        UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:self.turnTable.path];

        CGPoint p = [self.touchPoint CGPointValue];

        CGPoint p1 = [[self.turnTable presentationLayer] convertPoint:p fromLayer:self.view.layer];

        if (self.previousPoint) {

            CGPoint p0 = [self.previousPoint CGPointValue];

            [path moveToPoint:p0];

            [path addLineToPoint:p1];

            self.turnTable.path = path.CGPath;

        }

        self.previousPoint = [NSValue valueWithCGPoint:p1];

    }

}

#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0xF0CC9C);

        case 1:

            return UIColorHex(0x271103);

        case 2:

            return UIColorHex(0x593922);

        default:

            break;

    }

    return nil;

}

@end