Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. @IBOutlet weak var Gameview: SKView!
  2.  
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. let scene = GameScene(size:Gameview.frame.size)
  6. Gameview.presentScene(scene)
  7. }
  8.  
  9. class GameScene: SKScene, UIGestureRecognizerDelegate {
  10.  
  11. var newCamera: SKCameraNode!
  12.  
  13. let rotateRec = UIRotationGestureRecognizer()
  14. let zoomRec = UIPinchGestureRecognizer()
  15. //let panRec = UIPanGestureRecognizer()
  16.  
  17.  
  18. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
  19. shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer)
  20. -> Bool {return true}
  21.  
  22.  
  23.  
  24. override func didMove(to view: SKView) {
  25.  
  26. rotateRec.addTarget(self, action: #selector(GameScene.rotatedView (_:) ))
  27. rotateRec.delegate = self
  28. self.view!.addGestureRecognizer(rotateRec)
  29.  
  30. zoomRec.addTarget(self, action: #selector(GameScene.zoomedView (_:) ))
  31. zoomRec.delegate = self
  32. self.view!.addGestureRecognizer(zoomRec)
  33.  
  34. //panRec.addTarget(self, action: #selector(GameScene.pannedView (_:) ))
  35. //self.view!.addGestureRecognizer(panRec)
  36.  
  37.  
  38. newCamera = SKCameraNode()
  39. newCamera.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
  40. self.addChild(newCamera)
  41. self.camera = newCamera
  42.  
  43. //drawBoard()
  44. //I removed this function as it had nothing to do with the question
  45.  
  46. }
  47.  
  48. @objc func rotatedView(_ sender:UIRotationGestureRecognizer) {
  49. print(sender.location(in: self.view))
  50. let square = SKSpriteNode(color: #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1), size: CGSize(width: 10, height: 10))
  51. let a = sender.location(in: self.view)
  52. let b = -a.x
  53. let c = -a.y
  54. let d = CGPoint(x: b, y: c)
  55.  
  56.  
  57. square.position = d
  58. self.addChild(square)
  59.  
  60.  
  61. newCamera.zRotation += sender.rotation
  62. sender.rotation = 0
  63. if sender.state == .ended {
  64. let rotateAmount = abs(Double(newCamera.zRotation) * Double(180) / Double.pi)
  65. let remainder = abs(rotateAmount.truncatingRemainder(dividingBy: 90))
  66. //print(rotateAmount)
  67. //print(remainder)
  68. if remainder < 10 {
  69. newCamera.zRotation = CGFloat((rotateAmount-remainder)*Double.pi/180)
  70. } else if remainder > 80 {
  71. newCamera.zRotation = CGFloat((rotateAmount-remainder+90)*Double.pi/180)
  72. }
  73.  
  74. }
  75.  
  76. }
  77.  
  78.  
  79.  
  80. @objc func zoomedView(_ sender:UIPinchGestureRecognizer) {
  81. let pinch = SKAction.scale(by: 1/sender.scale, duration: 0.0)
  82. newCamera.run(pinch)
  83. sender.scale = 1.0
  84. }
  85.  
  86. //@objc func pannedView(_ sender:UIPanGestureRecognizer) {
  87. // sender.translation(in: )
  88. //}
  89.  
  90.  
  91. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  92. for touch in touches {
  93. let location = touch.location(in: self)
  94. let previousLocation = touch.previousLocation(in: self)
  95. let deltaY = location.y - previousLocation.y
  96. let deltax = location.x - previousLocation.x
  97. newCamera!.position.y -= deltaY
  98. newCamera!.position.x -= deltax
  99. }
  100. }
  101. }
  102.  
  103. let bluesquare = SKSpriteNode(color: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1), size: CGSize(width: 10, height: 10))
  104.  
  105. let a = sender.location(in: self.view)
  106. bluesquare.position = self.convertPoint(fromView: a)
  107.  
  108. self.addChild(bluesquare)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement