iPhoneカードをひく

真ん中にある山からカードをひくiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface DrawScene : SKScene

@property (nonatomic, weak) SKNode *selectedCard;

@property int counter;

@end

@implementation DrawScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [UIColor greenColor];

    [self createCards];

    [self createDropArea];

}

– (void)createCards

{

    float w = 40;

    float h = w * 1.618;

    CGPoint o = CGPointMake(160, 350);

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-w/2.0, -h/2.0, w, h) cornerRadius:5];

    

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

        float r = arc4random() % 60 + 30;

        float x = r * cos(i * M_PI / 5) + o.x;

        float y = r * sin(i * M_PI / 5) + o.y;

        SKShapeNode *card = [SKShapeNode node];

        card.position = CGPointMake(x, y);

        card.name = [NSString stringWithFormat:@”card%d”,i];

        card.path = path.CGPath;

        card.lineWidth = 3;

        card.fillColor = [SKColor grayColor];

        card.zPosition = i + 1;

        [self addChild:card];

    }

}

– (void)createDropArea

{

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-50, –50, 100, 100) cornerRadius:10];

    SKShapeNode *drop = [SKShapeNode node];

    drop.name = @”drop area”;

    drop.path = path.CGPath;

    drop.lineWidth = 5;

    drop.position = CGPointMake(160, 170);

    drop.fillColor = [SKColor lightGrayColor];

    [self addChild:drop];

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    

    int max = 0;

    for (SKNode *node in self.children) {

        if ([[node.name substringToIndex:4] isEqual:@”card”] && [node containsPoint:p]) {

            self.selectedCard = node;

        }

        

        if (max < node.zPosition) {

            max = node.zPosition;

        }

    }

    self.selectedCard.zPosition = max + 1;

    

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    self.selectedCard.position = p;

}

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

{

    SKNode *dropArea = [self childNodeWithName:@”drop area”];

    if (CGRectIntersectsRect(dropArea.frame, self.selectedCard.frame)) {

        [self drawCard:self.selectedCard];

    }

    

    self.selectedCard = nil;

}

– (void)drawCard:(SKNode *)card

{

    SKAction *flipA = [SKAction scaleXTo:0 duration:0.2];

    SKAction *drawBack = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node, CGFloat elapsedTime) {

        SKLabelNode *number = [SKLabelNode node];

        number.text = [card.name substringFromIndex:4];

        [(SKShapeNode *)node setFillColor:[SKColor colorWithWhite:0.8 alpha:1]];

        [node addChild:number];

    }];

    SKAction *flipB = [SKAction scaleXTo:1.0 duration:0.2];

    SKAction *wait = [SKAction waitForDuration:1.0];

    

    float x = (self.counter % 5) * 50 + 50;

    float y = (self.counter / 5) * (-40) + 70;

    SKAction *move = [SKAction moveTo:CGPointMake(x, y) duration:0.5];

    [card runAction:[SKAction sequence:@[flipA, drawBack, flipB, wait, move]]];

    

    self.counter++;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[DrawScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end