iPhoneお絵書きボタン

数字をタップしてお絵書きしていくiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SpriteKit;

@interface TraceScene : SKScene

@end

@implementation TraceScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor grayColor];

    [self createLineArea];

    [self createButtonTable];

    [self createMarker];

}

– (void)createLineArea

{

    SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(CGRectGetMaxX(self.frame), CGRectGetMidY(self.frame))];

    n.position = CGPointMake(CGRectGetMidX(self.frame), 3.0 * CGRectGetMidY(self.frame)/2.0);

    n.name = @”line area”;

    [self addChild:n];

}

– (void)createButtonTable

{

    float w = CGRectGetMaxX(self.frame);

    float h = CGRectGetMidY(self.frame);

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

        float x = (i % 5) * (w / 5.0) + w/10.0;

        float y = h – ((i / 5) * (h / 5.0) + h/10.0);

        SKLabelNode *l = [SKLabelNode node];

        l.name = @”number button”;

        l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

        l.text = [@(i+1) stringValue];

        l.position = CGPointMake(x, y);

        l.fontColor = [SKColor colorWithHue:0.04 * i saturation:0.9 brightness:0.8 alpha:1];

        l.fontName = @”Chalkduster”;

        l.zPosition = 1;

        [self addChild:l];

    }

}

– (void)createMarker

{

    UIBezierPath *path = [UIBezierPath bezierPath];

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

        float x = 10.0 * cos(M_PI/2.5 * i);

        float y = 10.0 * sin(M_PI/2.5 * i);

        [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:10 startAngle:0 endAngle:2.0*M_PI clockwise:NO]];

    }

    SKShapeNode *marker = [SKShapeNode node];

    marker.name = @”marker”;

    marker.path = path.CGPath;

    marker.position = [self childNodeWithName:@”line area”].position;

    [self addChild:marker];

    

    [marker runAction:[SKAction repeatActionForever:[SKAction rotateByAngle:1 duration:1.0]]];

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    SKNode *hit = [self nodeAtPoint:p];

    if ([hit.name isEqual:@”number button”]) {

        [hit runAction:[SKAction sequence:@[[SKAction fadeOutWithDuration:0.2], [SKAction fadeInWithDuration:0.2]]]];

        SKNode *marker = [self childNodeWithName:@”marker”];

        CGPoint p = CGPointMake(hit.position.x, hit.position.y + CGRectGetMidY(self.frame));

        

        UIBezierPath *path = [UIBezierPath bezierPath];

        [path moveToPoint:marker.position];

        [path addLineToPoint:p];

        SKShapeNode *line = [SKShapeNode node];

        line.path = path.CGPath;

        line.strokeColor = ((SKLabelNode *)hit).fontColor;

        [self addChild:line];

        

        

        [marker runAction:[SKAction moveTo:p duration:0.2]];

    }

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    SKScene *scene = [[TraceScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end