iPhoneパラパラアニメ

片手を上げる動作、全6枚の画像をパラパラアニメで表示するiPhoneアプリを作ってみる。


動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface ParaparaScene : SKScene

@property BOOL contentCreated;

@property NSArray *handupFrames;

@end

@implementation ParaparaScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

        self.backgroundColor = [UIColor lightGrayColor];

    }

}

– (void)createSceneContents

{

    self.handupFrames = [self animationFramesForImageNamePrefix:@”lego” frameCount:6];

    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:[self.handupFrames objectAtIndex:0]];

    sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    [self addChild:sprite];

    

    SKAction *animationFramesAction = [SKAction animateWithTextures:self.handupFrames timePerFrame:0.2 resize:YES restore:NO];

    [sprite runAction:[SKAction repeatActionForever:animationFramesAction]];

    

    [self addDescription:@”flick book (SpriteKit)”];

}

– (NSArray *)animationFramesForImageNamePrefix:(NSString *)baseImageName frameCount:(NSInteger)count

{

    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:count];

    for (int i=1; i<=count; i++) {

        NSString *imageName = [NSString stringWithFormat:@”%@%d.png”, baseImageName, i];

        SKTexture *t = [SKTexture textureWithImageNamed:imageName];

        [arr addObject:t];

    }

    return arr;

}

– (void)addDescription:(NSString*)description

{

    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@”Helvetica”];

    myLabel.text = description;

    myLabel.fontSize = 18;

    myLabel.position = CGPointMake(160, 80);

    [self addChild:myLabel];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[ParaparaScene alloc] initWithSize:CGSizeMake(320, 568)];

    scene.scaleMode = SKSceneScaleModeAspectFit;

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end