
円柱状に回転しながらページングするiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createPage];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [self color:0];
sv.scene = [SCNScene scene];
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createPage
{
SCNBox *box = [SCNBox boxWithWidth:40 height:40 length:0 chamferRadius:0];
int num = arc4random() % 9;
UIColor *color = [self color:(num % 4) + 1];
CATextLayer *l = [CATextLayer layer];
l.string = [NSString stringWithFormat:@”< %d >”, num];
l.alignmentMode = kCAAlignmentCenter;
l.foregroundColor = color.CGColor;
l.fontSize = 120;
l.frame = CGRectMake(0, 0, 400, 400);
l.backgroundColor = [color colorWithAlphaComponent:0.2].CGColor;
box.firstMaterial.diffuse.contents = l;
box.firstMaterial.specular.contents = [[self color:1] colorWithAlphaComponent:0.1];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.name = @”nextpage”;
boxNode.pivot = SCNMatrix4MakeTranslation(0, 0, 40);
boxNode.opacity = 0;
[self.sceneView.scene.rootNode addChildNode:boxNode];
}
– (void)createCamera
{
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.camera.zFar = 200;
camera.position = SCNVector3Make(0, 0, 120);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SCNNode *page = [self.sceneView.scene.rootNode childNodeWithName:@”nextpage” recursively:false];
[page runAction:[SCNAction fadeInWithDuration:0.2]];
[SCNTransaction begin];
[SCNTransaction setAnimationDuration:0.8];
page.transform = SCNMatrix4Rotate(page.transform, M_PI, 0, 1, 0);
[SCNTransaction commit];
SCNNode *currentPage = [self.sceneView.scene.rootNode childNodeWithName:@”currentpage” recursively:false];
[SCNTransaction begin];
[SCNTransaction setAnimationDuration:0.6];
currentPage.transform = SCNMatrix4Rotate(currentPage.transform, –M_PI*0.45, 0, 1, 0);
[SCNTransaction setCompletionBlock:^{
[currentPage removeFromParentNode];
[self createPage];
}];
[SCNTransaction commit];
page.name = @”currentpage”;
}
#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
{
switch (i) {
case 0: return ColorHex(0x95ECF2);
case 1: return ColorHex(0x11A1C4);
case 2: return ColorHex(0x1869AD);
case 3: return ColorHex(0x1013BA);
case 4: return ColorHex(0x1143C4);
}
return nil;
}
@end