Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created October 10, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/61c81a4978ed520f41f24bbad76fe53d to your computer and use it in GitHub Desktop.
Save mzsima/61c81a4978ed520f41f24bbad76fe53d to your computer and use it in GitHub Desktop.
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
@mzsima
Copy link
Author

mzsima commented Oct 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment