iPhoneもじロール

映画のスタッフロールみたいなiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController () <SCNSceneRendererDelegate>

@property (nonatomic, weak) SCNView *sceneView;

@property (nonatomic) NSTimeInterval interval;

@property (nonatomic) BOOL ready;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    

    [self setupScene];

    [self createText];

    [self createCamera];

}

– (void)setupScene {

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

    sv.delegate = self;

    sv.backgroundColor = [self color:0];

    sv.scene = [SCNScene scene];

    sv.scene.paused = YES;

    self.ready = NO;

    [self.view addSubview:sv];

    

    sv.allowsCameraControl = YES;

    self.sceneView = sv;

}

– (void)createText {

    NSArray *arr = @[@”X”, @”_”, @”A”,@”B”,@”C”,@”D”,@”E”,@”F”,@”G”,@”H”,@”I”];

    int size = arc4random_uniform(10) + 20;

    

    NSMutableString *mstr = [NSMutableString string];

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

        int random = arc4random() % arr.count;

        [mstr appendString:arr[random]];

    }

    

    SCNText *text = [SCNText textWithString:mstr extrusionDepth:0.3];

    text.firstMaterial.diffuse.contents = [[self color:1] colorWithAlphaComponent:0.8];

    text.firstMaterial.specular.contents = [self color:2];

    text.containerFrame = CGRectMake(-40, –10, 80, 20);

    text.alignmentMode = kCAAlignmentCenter;

    text.truncationMode = kCATruncationEnd;

    text.wrapped = YES;

    

    SCNNode *textNode = [SCNNode nodeWithGeometry:text];

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

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

    

    [textNode runAction:[SCNAction sequence:@[[SCNAction moveByX:0 y:200 z:0 duration:10.0], [SCNAction fadeOutWithDuration:0.3], [SCNAction runBlock:^(SCNNode *node) { [node removeFromParentNode]; }]]]];

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.camera.zFar = 1000;

    camera.camera.yFov = 150;

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

    camera.rotation = SCNVector4Make(1, 0, 0, 0.3);

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

}

– (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        self.interval = time;

    });

    

    if (time – self.interval > 0.8 && self.ready) {

        [self createText];

        self.interval = time;

        return;

    }

}

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

{

    self.sceneView.scene.paused = NO;

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

        self.ready = YES;

    });

}

#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[] = {0x040014, 0xFFEF00, 0xFFFAF4};

    return ColorHex(colorCode[i]);

}

@end