Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // ConvexLine
  4. //
  5. // Created by MizushimaYusuke on 2017/09/24.
  6. // Copyright © 2017 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SpriteKit
  11.  
  12. class ViewController: UIViewController {
  13.  
  14. weak var scene: SKScene?
  15.  
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. setupScene()
  19. createLines()
  20. }
  21.  
  22. func setupScene() {
  23. let sv = SKView(frame: view.bounds)
  24. let s = SKScene(size: sv.frame.size)
  25. sv.presentScene(s)
  26. view.addSubview(sv)
  27. scene = s
  28. }
  29.  
  30. func createLines() {
  31. stride(from: 0, through: view.frame.maxY, by: 40).enumerated().forEach { i, y in
  32. let line = createConvexLine()
  33. line.position = CGPoint(x: view.center.x, y: y)
  34. let d = view.frame.maxX
  35. if (i%2 == 0) {
  36. line.zRotation = .pi
  37. }
  38. line.childNode(withName: "maskBar")?.run(.sequence([.wait(forDuration: 10.0), .moveBy(x: d, y: 0, duration: 10.0)]))
  39.  
  40. }
  41. }
  42.  
  43. func createConvexLine() -> SKNode {
  44. UIGraphicsBeginImageContextWithOptions(CGSize(width: 80, height: 40), false, 0)
  45. UIColor(hue: 0.5, saturation: 0.9, brightness: 0.7, alpha: 1).set()
  46. if let ctx = UIGraphicsGetCurrentContext() {
  47. ctx.fill(CGRect(x: 0, y: 0, width: 80, height: 40))
  48. ctx.setBlendMode(.clear)
  49. ctx.fill(CGRect(x: 20, y: 20, width: 40, height: 16))
  50. ctx.fill(CGRect(x: 30, y: 4, width: 20, height: 30))
  51. }
  52. let img = UIGraphicsGetImageFromCurrentImageContext()!
  53. UIGraphicsEndImageContext()
  54.  
  55. let texture = SKTexture(image: img)
  56. let node = SKNode()
  57. stride(from: 0, through: view.frame.maxX, by: 80).forEach { x in
  58. let part = SKSpriteNode(texture: texture)
  59. part.position = CGPoint(x: x - view.frame.midX, y: 0)
  60. node.addChild(part)
  61. }
  62. node.position = view.center
  63. scene?.addChild(node)
  64.  
  65. let maskBar = SKSpriteNode(color: scene!.backgroundColor, size: CGSize(width: view.frame.maxX, height: 40))
  66. maskBar.name = "maskBar"
  67. node.addChild(maskBar)
  68.  
  69. return node
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement