まるとばつ、タッチでプニュっと形が変わるiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface ViewController () <SKSceneDelegate>
@property (nonatomic, weak) SKScene *scene;
@property (nonatomic, weak) SKNode *ox;
@property (nonatomic, weak) SKNode *xo;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createOX:CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
}
– (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *scene = [SKScene sceneWithSize:sv.frame.size];
scene.delegate = self;
scene.backgroundColor = [UIColor orangeColor];
[sv presentScene:scene];
[self.view addSubview:sv];
self.scene = scene;
}
– (SKNode *)createOX:(CGPoint)p {
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:20 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *circle = [SKShapeNode shapeNodeWithPath:path.CGPath];
circle.name = @”ox”;
circle.position = p;
circle.strokeColor = [UIColor whiteColor];
circle.fillColor = [UIColor clearColor];
circle.lineWidth = 3;
[self.scene addChild:circle];
return circle;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self.scene];
NSArray *hits = [self.scene nodesAtPoint:p];
for (SKNode *hit in hits) {
if ([hit.name isEqual:@”ox”]) {
hit.userData = [@{@”count”: @10} mutableCopy];
self.ox = hit;
return;
} else if ([hit.name isEqual:@”xo”]) {
hit.userData = [@{@”count”: @0} mutableCopy];
self.xo = hit;
return;
}
}
[self createOX:p];
}
– (void)update:(NSTimeInterval)currentTime forScene:(SKScene *)scene
{
if (self.ox) {
int count = [self.ox.userData[@”count”] intValue];
// stop o -> – animation
if (count < 0) {
CGPoint p = self.ox.position;
[self.ox removeFromParent];
SKSpriteNode *x = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(40, 40)];
x.name = @”xo”;
x.position = p;
[self.scene addChild:x];
for (int i=0; i<2; i++) {
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(40, 3)];
[x addChild:bar];
[bar runAction:[SKAction rotateToAngle: (i==0) ? M_PI*0.25 : –M_PI*0.25 duration:0.3]];
}
return;
}
// animation o -> –
[self.ox.userData setObject:@(–count) forKey:@”count”];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –1.0 * MAX(2.0 * count, 1.5), 40, MAX(4.0 * count, 3))];
((SKShapeNode *)self.ox).path = path.CGPath;
}
if (self.xo) {
int count = [self.xo.userData[@”count”] intValue];
if (count == 0) {
[self.xo.userData setObject:@1 forKey:@”count”];
for (SKNode *n in self.xo.children) {
[n runAction:[SKAction rotateToAngle: 0 duration:0.3]];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.xo.userData setObject:@2 forKey:@”count”];
});
return;
}
if (count == 1) {
return;
}
// after x -> –
if (count == 2) {
CGPoint p = self.xo.position;
[self.xo removeFromParent];
self.xo = [self createOX:p];
self.xo.userData = [@{@2 : @”count”} mutableCopy];
}
// stop animation
if (count > 11) {
self.xo.name = @”ox”;
self.xo = nil;
return;
}
// animation – -> o
[self.xo.userData setObject:@(++count) forKey:@”count”];
count = count – 2;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –1.0 * MAX(2.0 * count, 1.5), 40, MAX(4.0 * count, 3))];
((SKShapeNode *)self.xo).path = path.CGPath;
}
}
@end