Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // FancyButton
  4. //
  5. // Created by MizushimaYusuke on 2017/03/30.
  6. // Copyright © 2017 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. @import SpriteKit;
  11.  
  12. @interface ViewController ()
  13. @property (nonatomic, weak) SKScene *scene;
  14. @end
  15.  
  16. @implementation ViewController
  17.  
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. [self setupScene];
  21. [self createButtons];
  22. }
  23.  
  24. - (void)setupScene {
  25. SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
  26. SKScene *s = [SKScene sceneWithSize:sv.frame.size];
  27. [sv presentScene:s];
  28. [self.view addSubview:sv];
  29. self.scene = s;
  30. }
  31.  
  32. - (void)createButtons {
  33. float d = CGRectGetMaxX(self.view.bounds) / 8.0;
  34. for (int i=0; i<50; i++) {
  35. float x = (i % 8) * d + 40;
  36. float y = (i / 8) * d;
  37. SKNode *button = [self createButton];
  38. button.position = CGPointMake(x, y);
  39. }
  40. }
  41.  
  42. - (SKNode *)createButton {
  43. float l = arc4random_uniform(50) + 30;
  44. NSString *shapeType = arc4random_uniform(2) == 0 ? @"circle" : @"rect";
  45. float hue = arc4random_uniform(10) * 0.1;
  46. UIColor *colorA = [UIColor colorWithHue:hue saturation:0.5 brightness:1 alpha:1];
  47. UIColor *colorB = [UIColor colorWithHue:hue saturation:0.5 brightness:0.8 alpha:1];
  48.  
  49. UIGraphicsBeginImageContextWithOptions(CGSizeMake(l, l), NO, 0);
  50. UIBezierPath *path;
  51. if ([shapeType isEqual:@"circle"]) {
  52. path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, l, l)];
  53. } else {
  54. path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, l, l) cornerRadius:l * 0.1];
  55. }
  56. path.lineWidth = 3;
  57. [colorA set];
  58. [path fill];
  59. [colorB set];
  60. [path stroke];
  61.  
  62. CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
  63.  
  64. float r = l * 0.2;
  65. for (int i=0; i<4; i++) {
  66. float x = r * cos(M_PI * 0.5 * i) + l * 0.5;
  67. float y = r * sin(M_PI * 0.5 * i) + l * 0.5;
  68. CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(x - l * 0.05, y - l * 0.05, l * 0.1, l * 0.1));
  69. }
  70.  
  71. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  72. UIGraphicsEndImageContext();
  73.  
  74. SKTexture *texture = [SKTexture textureWithImage:img];
  75. SKSpriteNode *node = [SKSpriteNode spriteNodeWithTexture:texture];
  76. node.name = @"button";
  77. [self.scene addChild:node];
  78.  
  79. return node;
  80. }
  81.  
  82. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  83. [self.scene enumerateChildNodesWithName:@"button" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
  84. float duration = arc4random_uniform(60) * 0.1 + 0.5;
  85. [node runAction:[SKAction rotateByAngle:2.0 * M_PI duration:duration]];
  86. }];
  87. }
  88.  
  89. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement