点to零

点を数字のゼロに変形させるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@property (nonatomic) int count;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createZero];

}

– (void)setupScene {

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

    sv.scene = [SCNScene scene];

    sv.backgroundColor = [self color:0];

    sv.autoenablesDefaultLighting = YES;

    sv.allowsCameraControl = YES;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createZero {

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

        for (int j=0; j<9; j++) {

            if (j == 4) continue;

            SCNBox *b = [SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0.1];

            b.firstMaterial.diffuse.contents = [self color:i];

            SCNNode *bNode = [SCNNode nodeWithGeometry:b];

            bNode.name = [NSString stringWithFormat:@”%d”, j];

            bNode.position = SCNVector3Make(i * 4, 0, 0);

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

        }

    }

}

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

{

    if (self.count == 0) {

        [self.sceneView.scene.rootNode.childNodes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(SCNNode *node, NSDictionary *bindings) {

            if ([@[@”2″, @”5″, @”8″] indexOfObject:node.name] != NSNotFound) {

                [node runAction:[SCNAction moveByX:-1 y:0 z:0 duration:0.5]];

            } else if ([@[@”0″, @”3″, @”6″] indexOfObject:node.name] != NSNotFound) {

                [node runAction:[SCNAction moveByX:1 y:0 z:0 duration:0.5]];

            }

            return NO;

        }]];

    }

    

    else if (self.count == 1) {

        [self.sceneView.scene.rootNode.childNodes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(SCNNode *node, NSDictionary *bindings) {

            if ([@[@”0″, @”1″, @”2″] indexOfObject:node.name] != NSNotFound) {

                [node runAction:[SCNAction moveByX:0 y:1.1 z:0 duration:0.5]];

            } else if ([@[@”6″, @”7″, @”8″] indexOfObject:node.name] != NSNotFound) {

                [node runAction:[SCNAction moveByX:0 y:-1.1 z:0 duration:0.5]];

            }

            return NO;

        }]];

    }

    

    else if (self.count == 2) {

        [self.sceneView.scene.rootNode.childNodes enumerateObjectsUsingBlock:^(SCNNode *n, NSUInteger idx, BOOL *stop) {

            n.physicsBody = [SCNPhysicsBody dynamicBody];

        }];

        

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [self.sceneView.scene.rootNode.childNodes enumerateObjectsUsingBlock:^(SCNNode *n, NSUInteger idx, BOOL *stop) {

                [n removeFromParentNode];

            }];

            [self createZero];

        });

    }

    

    

    self.count = (self.count + 1) % 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 > 4) return nil;

    int colorCode[] = {0x979926, 0x38CCB5, 0xEEFF8E, 0xFFD767, 0xCC2A09};

    return ColorHex(colorCode[i]);

}

@end