#import “ViewController.h”
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@property (nonatomic) int count;
@property (nonatomic, strong) NSMutableArray *data;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createData];
[self createNode];
}
– (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
[sv presentScene:s];
[self.view addSubview:sv];
self.scene = s;
}
– (void)createNode {
for (int i=0; i < 6; i++) {
SKNode *base = [SKNode node];
base.name = [NSString stringWithFormat:@”base%d”, i];
base.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
[self.scene addChild:base];
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[UIColor grayColor] size:CGSizeMake(60, 10)];
bar.name = @”bar”;
bar.position = CGPointMake(0, 100);
[base addChild:bar];
}
}
– (void)createData {
self.data = [NSMutableArray array];
[self.data addObject:@[@20, @4, @0, @12, @12]]; // 1-2, 1-3, 1-4, 1-5, 1-6
[self.data addObject:@[@1, @15, @12, @12, @20]]; // 2-3, 2-4, 2-5, 2-6, 2-1
[self.data addObject:@[@20, @12, @22, @4, @1]]; // 3-4, 3-5, 3-6, 3-1, 3-2
[self.data addObject:@[@9, @12, @0, @15, @12]];// 4-5, 4-6, 4-1, 4-2, 4-3
[self.data addObject:@[@15, @12, @12, @12, @9]];
}
– (void)showLines {
for (int i=0; i<5; i++) {
for (int j=0; j<5-i; j++) {
float x0 = 0;
for (int n=0; n<i; n++) {
x0 += [self.data[j][n] floatValue];
}
float x1 = 0;
for (int n=0; n<i; n++) {
x1 += [self.data[(j + i + 1) % 5][4 – n] floatValue];
}
SKNode *bar0 = [[self.scene childNodeWithName:[NSString stringWithFormat:@”base%d”, j]] childNodeWithName:@”bar”];
SKNode *bar1 = [[self.scene childNodeWithName:[NSString stringWithFormat:@”base%d”, (j + i + 1) % 6]] childNodeWithName:@”bar”];
CGPoint p1 = [bar0.parent convertPoint:CGPointMake(bar0.position.x + x0 – 30, bar0.position.y – 5) toNode:self.scene];
CGPoint p2 = [bar1.parent convertPoint:CGPointMake(bar1.position.x – x1 + 30, bar1.position.y – 5) toNode:self.scene];
x0 += [self.data[j][i] floatValue];
x1 += [self.data[(j + i + 1) % 5][4 – i] floatValue];
CGPoint p3 = [bar0.parent convertPoint:CGPointMake(bar0.position.x + x0 – 30, bar0.position.y – 5) toNode:self.scene];
CGPoint p4 = [bar1.parent convertPoint:CGPointMake(bar1.position.x – x1 + 30, bar1.position.y – 5) toNode:self.scene];
CGPoint o = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:p1];
[path addQuadCurveToPoint:p2 controlPoint:o];
[path addLineToPoint:p4];
[path addQuadCurveToPoint:p3 controlPoint:o];
SKShapeNode *line = [SKShapeNode shapeNodeWithPath:path.CGPath];
UIColor *color = [UIColor colorWithHue:0.2 * i saturation:0.5 brightness:1 alpha:1];
line.fillColor = color;
line.strokeColor = color;
[self.scene addChild:line];
line.alpha = 0;
[line runAction:[SKAction sequence:@[[SKAction waitForDuration:i * j * 0.1], [SKAction fadeInWithDuration:0.2]]]];
}
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.count == 0) {
float dw = M_PI / 3.0;
for (int i=0; i < 6; i++) {
SKNode *base = [self.scene childNodeWithName:[NSString stringWithFormat:@”base%d”, i]];
[base runAction:[SKAction rotateByAngle:i * dw duration:1.0]];
}
self.count++;
return;
}
for (int i=0; i<5; i++) {
for (int j=0; j<5-i; j++) {
float x0 = 0;
for (int n=0; n<i; n++) {
x0 += [self.data[j][n] floatValue];
}
float x1 = 0;
for (int n=0; n<i; n++) {
x1 += [self.data[(j + i + 1) % 5][4 – n] floatValue];
}
SKNode *bar0 = [[self.scene childNodeWithName:[NSString stringWithFormat:@”base%d”, j]] childNodeWithName:@”bar”];
SKNode *bar1 = [[self.scene childNodeWithName:[NSString stringWithFormat:@”base%d”, (j + i + 1) % 6]] childNodeWithName:@”bar”];
CGPoint p1 = [bar0.parent convertPoint:CGPointMake(bar0.position.x + x0 – 30, bar0.position.y – 5) toNode:self.scene];
CGPoint p2 = [bar1.parent convertPoint:CGPointMake(bar1.position.x – x1 + 30, bar1.position.y – 5) toNode:self.scene];
x0 += [self.data[j][i] floatValue];
x1 += [self.data[(j + i + 1) % 5][4 – i] floatValue];
CGPoint p3 = [bar0.parent convertPoint:CGPointMake(bar0.position.x + x0 – 30, bar0.position.y – 5) toNode:self.scene];
CGPoint p4 = [bar1.parent convertPoint:CGPointMake(bar1.position.x – x1 + 30, bar1.position.y – 5) toNode:self.scene];
CGPoint o = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:p1];
[path addQuadCurveToPoint:p2 controlPoint:o];
[path addLineToPoint:p4];
[path addQuadCurveToPoint:p3 controlPoint:o];
SKShapeNode *line = [SKShapeNode shapeNodeWithPath:path.CGPath];
UIColor *color = [UIColor colorWithHue:0.2 * i saturation:0.5 brightness:1 alpha:1];
line.fillColor = color;
line.strokeColor = color;
[self.scene addChild:line];
line.alpha = 0;
[line runAction:[SKAction sequence:@[[SKAction waitForDuration:i * j * 0.1], [SKAction fadeInWithDuration:0.2]]]];
}
}
}
@end