Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // FancyButton
- //
- // Created by MizushimaYusuke on 2017/03/30.
- // 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 createButtons];
- }
- - (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)createButtons {
- float d = CGRectGetMaxX(self.view.bounds) / 8.0;
- for (int i=0; i<50; i++) {
- float x = (i % 8) * d + 40;
- float y = (i / 8) * d;
- SKNode *button = [self createButton];
- button.position = CGPointMake(x, y);
- }
- }
- - (SKNode *)createButton {
- float l = arc4random_uniform(50) + 30;
- NSString *shapeType = arc4random_uniform(2) == 0 ? @"circle" : @"rect";
- float hue = arc4random_uniform(10) * 0.1;
- UIColor *colorA = [UIColor colorWithHue:hue saturation:0.5 brightness:1 alpha:1];
- UIColor *colorB = [UIColor colorWithHue:hue saturation:0.5 brightness:0.8 alpha:1];
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(l, l), NO, 0);
- UIBezierPath *path;
- if ([shapeType isEqual:@"circle"]) {
- path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, l, l)];
- } else {
- path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, l, l) cornerRadius:l * 0.1];
- }
- path.lineWidth = 3;
- [colorA set];
- [path fill];
- [colorB set];
- [path stroke];
- CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
- float r = l * 0.2;
- for (int i=0; i<4; i++) {
- float x = r * cos(M_PI * 0.5 * i) + l * 0.5;
- float y = r * sin(M_PI * 0.5 * i) + l * 0.5;
- CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(x - l * 0.05, y - l * 0.05, l * 0.1, l * 0.1));
- }
- UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- SKTexture *texture = [SKTexture textureWithImage:img];
- SKSpriteNode *node = [SKSpriteNode spriteNodeWithTexture:texture];
- node.name = @"button";
- [self.scene addChild:node];
- return node;
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- [self.scene enumerateChildNodesWithName:@"button" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
- float duration = arc4random_uniform(60) * 0.1 + 0.5;
- [node runAction:[SKAction rotateByAngle:2.0 * M_PI duration:duration]];
- }];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement