iPhone 数字シリンダー

シリンダーに数字を貼り付けてクルクル回すiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setup];

    [self createCamera];

    [self createNumbers];

}

– (void)setup {

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

    sv.backgroundColor = [UIColor blackColor];

    sv.scene = [SCNScene scene];

    sv.allowsCameraControl = YES;

    [self.view addSubview:sv];

    self.sceneView = sv;

}

– (void)createCamera {

    SCNNode *cameraSpace = [SCNNode node];

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

    

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 5, 20);

    [cameraSpace addChildNode:camera];

    [cameraSpace runAction:[SCNAction rotateByX:0 y:M_PI z:0 duration:3.0]];

}

– (void)createNumbers {

    

    for (int i=1; i<10; i++) {

        SCNCylinder *c = [SCNCylinder cylinderWithRadius:3 height:1];

        float hue = (i % 2) == 0 ? i * 0.1 : 1 – i * 0.1;

        c.firstMaterial.diffuse.contents = [UIColor colorWithHue:hue saturation:0.6 brightness:1 alpha:0.9];

        SCNNode *cNode = [SCNNode nodeWithGeometry:c];

        cNode.position = SCNVector3Make(0, i * 1.1, 0);

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

        

        SCNText *t = [SCNText textWithString:[NSString stringWithFormat:@”%d”, i] extrusionDepth:1];

        SCNNode *tNode = [SCNNode nodeWithGeometry:t];

        tNode.pivot = SCNMatrix4MakeTranslation(0, 7, –30);

        tNode.scale = SCNVector3Make(0.1, 0.1, 0.1);

        

        [cNode addChildNode:tNode];

        [cNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:M_PI z:0 duration: sqrt(i)]]];

    }

}

@end