iPhone片輪車

車輪が半分くらい見えている動く和柄 iPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SpriteKit;

@interface ViewController ()

@property (nonatomic, weak) SKScene *scene;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createWave];

}

– (void)setupScene {

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

    SKScene *s = [SKScene sceneWithSize:sv.frame.size];

    s.backgroundColor = [UIColor colorWithHue:0.7 saturation:0.2 brightness:1 alpha:1];

    [sv presentScene:s];

    [self.view addSubview:sv];

    

    self.scene = s;

}

– (void)createWave {

    SKNode *wave = [SKNode node];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointZero];

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

        [path addQuadCurveToPoint:CGPointMake(i * 40 + 20, 0) controlPoint:CGPointMake(i * 40 + 10, 5)];

        [path addQuadCurveToPoint:CGPointMake(i * 40 + 40, 0) controlPoint:CGPointMake(i * 40 + 30, –5)];

    }

    [path addLineToPoint:CGPointMake(400, –10)];

    [path addLineToPoint:CGPointMake(0, –10)];

    [path closePath];

    

    SKShapeNode *waveU = [SKShapeNode shapeNodeWithPath:path.CGPath centered:YES];

    waveU.fillColor = [UIColor purpleColor];

    waveU.position = CGPointMake(0, 10);

    [wave addChild:waveU];

    

    SKShapeNode *waveB = [SKShapeNode shapeNodeWithPath:path.CGPath centered:YES];

    waveB.fillColor = [UIColor purpleColor];

    waveB.position = CGPointMake(0, –10);

    waveB.zRotation = M_PI;

    [wave addChild:waveB];

    

    SKSpriteNode *mid = [SKSpriteNode spriteNodeWithColor:[UIColor purpleColor] size:CGSizeMake(CGRectGetMaxX(self.view.bounds), 20)];

    [wave addChild:mid];

    

    wave.position = CGPointMake(CGRectGetMidX(self.view.bounds), 200);

    wave.zPosition = 10;

    [self.scene addChild:wave];

}

– (SKNode *)createWaguruma {

    

    SKNode *waguruma = [SKNode node];

    

    float dx = 2.0 * M_PI / 5.0;

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

        float start = i * dx;

        float end = (i + 0.8) * dx;

        UIBezierPath *path = [UIBezierPath bezierPath];

        [path addArcWithCenter:CGPointZero radius:20 startAngle:start endAngle:end clockwise:YES];

        SKShapeNode *ring = [SKShapeNode shapeNodeWithPath:path.CGPath];

        ring.fillColor = [UIColor clearColor];

        ring.lineWidth = 6;

        ring.strokeColor = [UIColor whiteColor];

        [waguruma addChild:ring];

        

        SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(12, 4)];

        bar.anchorPoint = CGPointMake(-0.5, 0.5);

        bar.zRotation = start – M_PI * 0.05;

        [waguruma addChild:bar];

    }

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path addArcWithCenter:CGPointZero radius:5 startAngle:0 endAngle:2.0 * M_PI clockwise:YES];

    SKShapeNode *cring = [SKShapeNode shapeNodeWithPath:path.CGPath];

    cring.fillColor = [UIColor clearColor];

    cring.lineWidth = 4;

    cring.strokeColor = [UIColor whiteColor];

    [waguruma addChild:cring];

    

    waguruma.position = CGPointMake(0, 0);

    [self.scene addChild:waguruma];

    

    return waguruma;

}

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

{

    SKNode *waguruma = [self createWaguruma];

    waguruma.position = CGPointMake(0, 225);

    [waguruma runAction:[SKAction group:@[[SKAction moveTo:CGPointMake(CGRectGetMaxX(self.view.bounds) + 100, 225) duration:3.0], [SKAction repeatActionForever:[SKAction rotateByAngle:-M_PI duration:1.0]]]] completion:^{

        [waguruma removeFromParent];

    }];

}

@end