Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 22, 2015 11:35
Show Gist options
  • Save mzsima/614fb0eae10045af7850 to your computer and use it in GitHub Desktop.
Save mzsima/614fb0eae10045af7850 to your computer and use it in GitHub Desktop.
addition ring
//
// ViewController.m
// AdditoinRing
//
// Created by Mizushima Yusuke on 8/22/15.
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved.
//
#import "ViewController.h"
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@property (nonatomic, weak) SKNode *ring;
@property (nonatomic, weak) SKLabelNode *valueLabel;
@property (nonatomic) int count;
@property (nonatomic) float ringW;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createAdditionRing];
[self updateFormula];
[self createButton];
}
- (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
s.backgroundColor = [UIColor colorWithHue:0.7 saturation:0.3 brightness:0.3 alpha:1];
[sv presentScene:s];
[self.view addSubview:sv];
self.scene = s;
}
- (void)createAdditionRing {
CGPoint o = CGPointMake(CGRectGetMidX(self.view.bounds), 400);
SKShapeNode *ring = [SKShapeNode shapeNodeWithCircleOfRadius:CGRectGetMidX(self.view.bounds) * 0.8];
ring.fillColor = [UIColor colorWithHue:0.5 saturation:0.3 brightness:0.3 alpha:1];
ring.lineWidth = 0;
ring.position = o;
[self.scene addChild:ring];
self.ring = ring;
[self.scene addChild:[self mylabel:@"+" p:CGPointMake(o.x - 60, o.y)]];
[self.scene addChild:[self mylabel:@"=" p:CGPointMake(o.x + 60, o.y)]];
NSString *valY = [NSString stringWithFormat:@"%d", arc4random() % 10];
SKLabelNode *val = [self mylabel:valY p:CGPointMake(o.x, o.y)];
[self.scene addChild:val];
self.valueLabel = val;
}
typedef struct {int y; int ringVal[8];} NumberSet;
- (void)updateFormula {
[self.ring removeAllChildren];
self.ringW = 0;
self.ring.zRotation = 0;
// A. 2 + 1 = 3
// B. 3 + 4 = 7
// C. 2 + 7 = 9
// D. 5 + 3 = 8
// E. 6 + 0 = 6
NumberSet s[] = {
{.y=1, .ringVal={1,2,6,4,5,3,8,7}},
{.y=4, .ringVal={3,2,6,4,7,1,8,9}},
{.y=7, .ringVal={2,3,6,7,9,8,5,1}},
{.y=3, .ringVal={2,0,6,7,5,8,1,3}},
{.y=0, .ringVal={2,6,1,5,8,6,3,1}},
};
NumberSet q = s[self.count];
self.valueLabel.text = [NSString stringWithFormat:@"%d", q.y];
float dw = M_PI / 4.0;
float r = 114;
for (int i=0; i<8; i++) {
float x = r * cos(dw * i);
float y = r * sin(dw * i);
NSString *s = [NSString stringWithFormat:@"%d", q.ringVal[i]];
SKLabelNode* l = [self mylabel:s p:CGPointMake(x, y)];
[self.ring addChild:l];
}
self.count = (self.count + 1) % 5;
}
- (SKLabelNode *)mylabel:(NSString *)text p:(CGPoint)p {
float fontSize = 60;
NSString *fontName = [[UIFont boldSystemFontOfSize:40] fontName];
SKLabelNode *l = [SKLabelNode labelNodeWithText:text];
l.fontName = fontName;
l.fontSize = fontSize;
l.position = p;
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
return l;
};
- (void)createButton {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@"OK" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:40];
[btn setTintColor:[UIColor whiteColor]];
[btn sizeToFit];
btn.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) - 200);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
}
- (void)next {
[self updateFormula];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.ringW += M_PI / 4.0;
[self.ring runAction:[SKAction rotateToAngle:self.ringW duration:0.3 shortestUnitArc:NO]];
[self.ring.children enumerateObjectsUsingBlock:^(SKNode *n, NSUInteger idx, BOOL *stop) {
[n runAction:[SKAction rotateToAngle:-self.ringW duration:0.3 shortestUnitArc:NO]];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment