輪切りにしてスライドさせる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 createRing];
[self createCamera];
[self createButton];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor orangeColor];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createRing {
SCNNode*(^ringbuilder)(int) = ^(int i){
SCNTube *tube = [SCNTube tubeWithInnerRadius:14 – (i+1)*2 outerRadius:14 – i * 2 height:14];
tube.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.1 * i saturation:0.4 brightness:1 alpha:1];
SCNNode *node = [SCNNode nodeWithGeometry:tube];
[self.sceneView.scene.rootNode addChildNode:node];
return node;
};
for (int i=0; i<5; i++) {
for (int j=0; j<3; j++) {
SCNNode *ring = ringbuilder(i);
ring.name = [NSString stringWithFormat:@”ring-%d-%d”,i, j];
ring.position = SCNVector3Make(0, –25 + 25 * j, 0);
}
}
}
– (void)createButton {
self.sceneView.overlaySKScene = [SKScene sceneWithSize:self.view.bounds.size];
SKSpriteNode *btn = [SKSpriteNode spriteNodeWithColor:[UIColor lightGrayColor] size:CGSizeMake(40, 40)];
btn.name = @”button”;
btn.position = CGPointMake(CGRectGetMaxX(self.view.bounds) – 60, 60);
[self.sceneView.overlaySKScene addChild:btn];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.camera.zFar = 200;
camera.position = SCNVector3Make(0, 0, 140);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self.sceneView.overlaySKScene];
SKNode *hit = [self.sceneView.overlaySKScene nodeAtPoint:p];
if ([hit.name isEqual:@”button”]) {
NSArray *target = [self.sceneView.scene.rootNode.childNodes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@”name like [c] %@”, @”ring*”]];
[target enumerateObjectsUsingBlock:^(SCNNode *n, NSUInteger idx, BOOL *stop) {
int i = [[n.name substringWithRange:NSMakeRange(5, 1)] intValue];
int j = [[n.name substringWithRange:NSMakeRange(7, 1)] intValue];
[n runAction:[SCNAction sequence:@[[SCNAction waitForDuration:2-j], [SCNAction moveBy:SCNVector3Make(0, –5 * i, 0) duration:0.5 * i]]]];
}];
}
}
@end