Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
clothespins
//
// ViewController.m
// Clothespins
//
// Created by MizushimaYusuke on 2017/10/10.
// Copyright © 2017 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createClothepins];
}
- (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
[sv presentScene:s];
[self.view addSubview:sv];
self.scene = s;
}
- (void)createClothepins {
float dx = CGRectGetMaxX(self.view.bounds) / 9.0;
for (int i=1; i<9; i++) {
SKNode *pin = [self createClothepin];
pin.position = CGPointMake(dx * i, self.view.center.y);
pin.name = @"pin";
}
}
- (SKNode *)createClothepin {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 50), false, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(8, 30)];
[path addLineToPoint:CGPointMake(10, 50)];
[path addLineToPoint:CGPointMake(0, 50)];
[path closePath];
[[UIColor whiteColor] set];
[path fill];
float hue = arc4random_uniform(10) * 0.1;
[[UIColor colorWithHue:hue saturation:0.8 brightness:1 alpha:1] set];
CGContextSetLineWidth(ctx, 3);
CGContextStrokeEllipseInRect(ctx, CGRectMake(7, 25, 6, 6));
CGContextSetBlendMode(ctx, kCGBlendModeClear);
CGContextFillEllipseInRect(ctx, CGRectMake(7, 40, 10, 10));
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
SKTexture *texture = [SKTexture textureWithImage:img];
SKNode *pin = [SKNode node];
[self.scene addChild:pin];
for (int i=0; i<2; i++) {
SKSpriteNode *n = [SKSpriteNode spriteNodeWithTexture:texture];
[pin addChild:n];
if (i==0) {
n.xScale = n.xScale * -1;
[n runAction:[SKAction repeatActionForever:[SKAction sequence:@[[SKAction rotateByAngle:0.3 duration:1.0], [SKAction rotateByAngle:-0.3 duration:1.0]]]]];
} else {
[n runAction:[SKAction repeatActionForever:[SKAction sequence:@[[SKAction rotateByAngle:-0.3 duration:1.0], [SKAction rotateByAngle:0.3 duration:1.0]]]]];
}
}
return pin;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.scene enumerateChildNodesWithName:@"pin" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
[node runAction:[SKAction rotateByAngle:M_PI * 0.5 duration:0.5]];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment