iPhone数かぞえ穴円盤

穴の空いた円盤でかずを表示するiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@import SpriteKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createBackBoard];

    [self createFrontBoard];

    [self createCamera];

    [self createLight];

}

– (void)setupScene {

    SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];

    sv.scene = [SCNScene scene];

    sv.backgroundColor = [self color:0];

    sv.overlaySKScene = [SKScene sceneWithSize:sv.frame.size];

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createBackBoard {

    SCNBox *box = [SCNBox boxWithWidth:40 height:32 length:1 chamferRadius:1];

    box.firstMaterial.diffuse.contents = [self color:2];

    SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

    boxNode.position = SCNVector3Make(0, 0, 0);

    [self.sceneView.scene.rootNode addChildNode:boxNode];

    

    int cnt = 5 * 3;

    float dangle = 2.0 * M_PI / (float)cnt;

    float r = 11;

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

        

        if (i == 3 * 2 + 1

            || i == 3 * 4 + 1

            || i == 3 * 3 + 2) {

            continue;

        }

        

        float x = r * cos(dangle * i);

        float y = r * sin(dangle * i);

        SCNBox *box = [SCNBox boxWithWidth:2 height:2 length:0.5 chamferRadius:0];

        box.firstMaterial.diffuse.contents = [self color:1];

        SCNNode *markNode = [SCNNode nodeWithGeometry:box];

        markNode.position = SCNVector3Make(x, y, 0.5);

        [boxNode addChildNode:markNode];

        

        [markNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByAngle:M_PI aroundAxis:SCNVector3Make(0, 0, 1) duration:1.0]]];

    }

}

– (void)createFrontBoard {

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

    int cnt = 5;

    float dangle = 2.0 * M_PI / (float)cnt;

    float r = 11;

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

        float x = r * cos(dangle * i);

        float y = r * sin(dangle * i);

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

    }

    path.flatness = 0.1;

     

    SCNShape *shape = [SCNShape shapeWithPath:path extrusionDepth:1];

    shape.firstMaterial.diffuse.contents = [self color:4];

    SCNNode *shapeNode = [SCNNode nodeWithGeometry:shape];

    shapeNode.name = @”front board”;

    shapeNode.position = SCNVector3Make(0, 0, 1);

    [self.sceneView.scene.rootNode addChildNode:shapeNode];

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 0, 60);

    [self.sceneView.scene.rootNode addChildNode:camera];

}

– (void)createLight {

    SCNLight *light = [SCNLight light];

    light.type = SCNLightTypeOmni;

    SCNNode *lightNode = [SCNNode node];

    lightNode.light = light;

    lightNode.position = SCNVector3Make(0, 0, 60);

    [self.sceneView.scene.rootNode addChildNode:lightNode];

}

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

{

    SCNNode *board = [self.sceneView.scene.rootNode childNodeWithName:@”front board” recursively:NO];

    [board runAction:[SCNAction rotateByX:0 y:0 z:M_PI/7.5 duration:0.2]];

    

    NSArray *number = @[@”5″, @”3″, @”4″];

    SKLabelNode *l = (SKLabelNode *)[self.sceneView.overlaySKScene childNodeWithName:@”count label”];

    if (!l) {

        l = [SKLabelNode labelNodeWithText:number[0]];

        l.name = @”count label”;

        l.fontSize = 80;

        l.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) – 30);

        l.fontColor = [self color:3];

        [self.sceneView.overlaySKScene addChild:l];

    }

    NSInteger idx = [number indexOfObject:l.text] + 1;

    l.text = [number objectAtIndex:(idx % 3)];

}

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

– (UIColor *)color:(int)i {

    if (i>5) return nil;

    int colorCode[] = {0x2C3E50, 0xFC4349, 0xD7DADB, 0x6DBCDB, 0xFFFFFF};

    return ColorHex(colorCode[i]);

}

@end