Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created June 11, 2017 14:01
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/a9245b208f385dea399a464a9702d887 to your computer and use it in GitHub Desktop.
Save mzsima/a9245b208f385dea399a464a9702d887 to your computer and use it in GitHub Desktop.
half color rect
//
// ViewController.m
// HalfColorRect
//
// Created by MizushimaYusuke on 2017/06/11.
// 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 createRect];
}
- (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
s.backgroundColor = [UIColor colorWithWhite:0.75 alpha:1];
[sv presentScene:s];
[self.view addSubview:sv];
self.scene = s;
}
- (void)createRect {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(-60, -60)];
[path addLineToPoint:CGPointMake(60, -60)];
[path addLineToPoint:CGPointMake(60, 60)];
[path closePath];
for (int i=0; i<4; i++) {
float hue = 0.2 * i;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(120, 120), NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 60, 60);
[[UIColor colorWithHue:hue saturation:0.9 brightness:0.9 alpha:1] set];
[path fill];
CGContextRotateCTM(ctx, M_PI);
[[UIColor colorWithHue:hue saturation:0.4 brightness:0.9 alpha:1] set];
[path fill];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
SKTexture *texture = [SKTexture textureWithImage:img];
for (int j=0; j<3; j++) {
SKSpriteNode *node =[SKSpriteNode spriteNodeWithTexture:texture];
node.name = @"rect";
node.position = CGPointMake(i * 120 + 150, CGRectGetMidY(self.view.bounds));
node.xScale = 1.0 - j * 0.3;
node.yScale = 1.0 - j * 0.3;
node.zRotation = M_PI * j;
[self.scene addChild:node];
}
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.scene enumerateChildNodesWithName:@"rect" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
if ([node.name isEqual:@"rect"] && arc4random_uniform(5) == 0) {
[node runAction:[SKAction rotateByAngle:M_PI * 0.5 duration:0.5]];
}
}];
}
@end
@mzsima
Copy link
Author

mzsima commented Jun 11, 2017

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