アルファベットのAの真ん中棒を回してFにするiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createA];
}
– (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:sv];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
s.backgroundColor = [UIColor colorWithHue:0.1 saturation:0.3 brightness:1 alpha:1];
[sv presentScene:s];
self.scene = s;
}
– (void)createA {
UIColor *color1 = [UIColor colorWithHue:0.4 saturation:0.5 brightness:1 alpha:1];
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointZero radius:100 startAngle:0 endAngle:M_PI clockwise:YES];
SKShapeNode *head = [SKShapeNode shapeNodeWithPath:path.CGPath];
head.name = @”head”;
head.position = CGPointMake(CGRectGetMidX(self.view.bounds), 300);
head.lineWidth = 20;
head.strokeColor = color1;
[self.scene addChild:head];
SKSpriteNode *r = [SKSpriteNode spriteNodeWithColor:color1 size:CGSizeMake(20, 100)];
r.anchorPoint = CGPointMake(0.5, 1.0);
r.name = @”right”;
r.position = CGPointMake(CGRectGetMidX(self.view.bounds) + 100, 300);
[self.scene addChild:r];
SKSpriteNode *l = [SKSpriteNode spriteNodeWithColor:color1 size:CGSizeMake(20, 100)];
l.position = CGPointMake(CGRectGetMidX(self.view.bounds) – 100, 250);
[self.scene addChild:l];
SKShapeNode *bar = [SKShapeNode shapeNodeWithRect:CGRectMake(-20, –10, 200, 20) cornerRadius:10];
bar.name = @”bar”;
bar.fillColor = [UIColor colorWithHue:0.8 saturation:0.01 brightness:1 alpha:1];
bar.position = CGPointMake(CGRectGetMidX(self.view.bounds) – 100, 280);
bar.zPosition = 10;
[self.scene addChild:bar];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *bar = [self.scene childNodeWithName:@”bar”];
SKNode *r = [self.scene childNodeWithName:@”right”];
SKNode *head = [self.scene childNodeWithName:@”head”];
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointZero radius:100 startAngle:1.5 * M_PI endAngle:2.0*M_PI clockwise:YES];
SKShapeNode *mask = [SKShapeNode shapeNodeWithPath:path.CGPath];
mask.lineWidth = 22;
mask.strokeColor = self.scene.backgroundColor;
mask.zPosition = 9;
mask.position = head.position;
[bar runAction:[SKAction rotateByAngle:2.0 * M_PI duration:2.0]];
[r runAction:[SKAction scaleXTo:1 y:0 duration:1.0] completion:^{
[self.scene addChild:mask];
[mask runAction:[SKAction rotateByAngle:0.3 * M_PI duration:1.0]];
}];
}
@end