iPhone飛び出るボタン

ボタンを押すとリングが飛び出るiPhoneアプリのサンプルコードを書いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [self color:3];

    [self setupScene];

    [self createPopupButton];

    [self createCamera];

    [self createLight];

}

– (void)setupScene {

    float w = CGRectGetMaxX(self.view.bounds);

    SCNView *sv = [[SCNView alloc] initWithFrame:CGRectMake(0, 0, w, w)];

    sv.scene = [SCNScene scene];

    sv.backgroundColor = [self color:0];

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createPopupButton {

    

    SCNBox *box = [SCNBox boxWithWidth:40 height:2 length:64 chamferRadius:1];

    box.firstMaterial.diffuse.contents = [self color:1];

    SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

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

    

    SCNCylinder *btn = [SCNCylinder cylinderWithRadius:8 height:0.1];

    btn.firstMaterial.diffuse.contents = [self color:2];

    SCNNode *btnNode = [SCNNode nodeWithGeometry:btn];

    btnNode.name = @”popbtn”;

    btnNode.position = SCNVector3Make(0, 2.5, –10);

    [boxNode addChildNode:btnNode];

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.camera.automaticallyAdjustsZRange = YES;

    camera.position = SCNVector3Make(5, 35, 80);

    camera.rotation = SCNVector4Make(0.5, 0, –0.4, –0.4);

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

}

– (void)createLight {

    SCNLight *light = [SCNLight light];

    light.type = SCNLightTypeOmni;

    SCNNode *lightNode = [SCNNode node];

    lightNode.light = light;

    lightNode.position = SCNVector3Make(0, 40, 20);

    lightNode.rotation = SCNVector4Make(1, 0, 0, –0.4);

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

}

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

    CGPoint p = [[touches anyObject] locationInView:self.sceneView];

    SCNNode *hit = [[self.sceneView hitTest:p options:@{SCNHitTestSortResultsKey:@YES}].firstObject node];

    if ([hit.name isEqual:@”popbtn”]) {

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

            

            SCNTube *ring = [SCNTube tubeWithInnerRadius:6.5 outerRadius:8 height:0.1];

            ring.firstMaterial.diffuse.contents = [[self color:4] colorWithAlphaComponent:0.8];

            SCNNode *ringNode = [SCNNode nodeWithGeometry:ring];

            ringNode.position = hit.position;

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

            [self animationRing:ringNode cnt:i];

        }

    }

}

– (void)animationRing:(SCNNode *)ring cnt:(int)i {

    [ring runAction:

     [SCNAction group:@[

                        [SCNAction moveBy:SCNVector3Make(0, 6, 0) duration:0.2],

                        [SCNAction scaleBy:1.1 duration:0.18]

                        ]

    ] completionHandler:^{

         int next = i – 1;

         if (next < 0) {

             [ring runAction:[SCNAction sequence:@[

                        [SCNAction fadeOutWithDuration:2],

                        [SCNAction runBlock:^(SCNNode *node) { [ring removeFromParentNode]; }]]]];

         } else {

             [self animationRing:ring cnt:next];

         }

     }];

}

#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000) >> 16)/255.0 green:((rgb & 0xFF00) >> 8)/255.0 blue:(rgb & 0xFF) /255.0 alpha:1]

– (UIColor *)color:(int)i {

    switch (i) {

        case 0 : return ColorHex(0x211426);

        case 1 : return ColorHex(0x413659);

        case 2 : return ColorHex(0x656F8C);

        case 3 : return ColorHex(0x9BBFAB);

        case 4 : return ColorHex(0xF2EFDF);

    }

    return nil;

}

@end