SpriteKitを使って、ぱかぱか点滅する信号を表示するiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface Signal : SKScene
@property BOOL contentCreated;
@end
@implementation Signal
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.scaleMode = SKSceneScaleModeAspectFit;
self.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:117.0/255.0 blue:112.0/255.0 alpha:1.0];
SKSpriteNode *whiteFrame = [[SKSpriteNode alloc] initWithColor:[SKColor whiteColor] size:CGSizeMake(250, 480)];
whiteFrame.position = CGPointMake(160, 285);
[self addChild:whiteFrame];
SKSpriteNode *darkRed = [[SKSpriteNode alloc] initWithColor:[SKColor colorWithRed:0.2 green:0 blue:0 alpha:1.0] size:CGSizeMake(200, 210)];
darkRed.position = CGPointMake(160, 400);
[self addChild:darkRed];
SKSpriteNode *red = [[SKSpriteNode alloc] initWithColor:[SKColor redColor] size:CGSizeMake(200, 210)];
red.position = CGPointMake(160, 400);
[self addChild:red];
SKSpriteNode *darkGreen = [[SKSpriteNode alloc] initWithColor:[SKColor colorWithRed:0 green:0.1 blue:0 alpha:1.0] size:CGSizeMake(200, 200)];
darkGreen.position = CGPointMake(160, 170);
[self addChild:darkGreen];
SKSpriteNode *green = [[SKSpriteNode alloc] initWithColor:[SKColor greenColor] size:CGSizeMake(200, 200)];
green.position = CGPointMake(160, 170);
[self addChild:green];
SKAction *waite = [SKAction waitForDuration:3];
SKAction *fadeOut = [SKAction fadeAlphaTo:0.2 duration:0.5];
SKAction *fadeIn = [SKAction fadeAlphaTo:1.0 duration:0.5];
SKAction *pulseRed = [SKAction sequence:@[waite,waite,fadeIn,waite,fadeIn,fadeOut,fadeIn,fadeOut,fadeIn,fadeOut]];
SKAction *pulseRedForever = [SKAction repeatActionForever: pulseRed];
[red runAction:pulseRedForever];
SKAction *pulseGreen = [SKAction sequence:@[fadeIn,waite,fadeIn,fadeOut,fadeIn,fadeOut,fadeIn,fadeOut,waite,waite]];
SKAction *pulseGreenForever = [SKAction repeatActionForever: pulseGreen];
[green runAction:pulseGreenForever];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
[spriteView presentScene:[[Signal alloc] initWithSize:self.view.bounds.size]];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end