Guest User

Untitled

a guest
Feb 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. class GameScene: SKScene, UIGestureRecognizerDelegate {
  2. var SceneCamera = SKCameraNode()
  3.  
  4. var LastLocation = CGPoint()
  5.  
  6. @objc func handlePanFrom(recognizer: UIPanGestureRecognizer) {
  7. var translation = recognizer.translation(in: recognizer.view!)
  8. translation = CGPoint(x: translation.x + LastLocation.x, y: -translation.y + LastLocation.y)
  9. camera?.position = CGPoint(x: position.x - translation.x, y: position.y - translation.y)
  10.  
  11. if recognizer.state == .ended {
  12. LastLocation = translation
  13. }
  14. }
  15.  
  16. @objc func handlePinchFrom(recognizer: UIPinchGestureRecognizer) {
  17.  
  18. var anchorPoint = recognizer.location(in: recognizer.view)
  19. anchorPoint = self.convertPoint(fromView: anchorPoint)
  20.  
  21. let anchorPointInCamera = camera?.convert(anchorPoint, from: self)
  22. camera?.setScale((camera?.xScale)! / (recognizer.scale))
  23.  
  24. let CameraAnchorPointInScene = self.convert(anchorPointInCamera!, from: camera!)
  25. let TranslationOfAnchorInScene = (x: anchorPoint.x - CameraAnchorPointInScene.x, y: anchorPoint.y - CameraAnchorPointInScene.y)
  26.  
  27. camera?.position = CGPoint(x: (camera?.position.x)! + TranslationOfAnchorInScene.x, y: (camera?.position.y)! + TranslationOfAnchorInScene.y)
  28. recognizer.scale = 1.0
  29.  
  30. setCameraConstraints()
  31. }
  32.  
  33. override func didMove(to view: SKView) {
  34.  
  35. // Setup camera control
  36. SceneCamera.position = CGPoint(x: (scene?.frame.midX)!, y: (scene?.frame.midY)!)
  37.  
  38. let playableAreaScaleForIphoneAndIpadPro = (view.bounds.size.height) / (scene?.frame.height)!
  39. SceneCamera.setScale(1/playableAreaScaleForIphoneAndIpadPro)
  40.  
  41. self.addChild(SceneCamera)
  42. self.camera = SceneCamera
  43.  
  44. setCameraConstraints()
  45.  
  46. // Camera Control: Pinch(Zoom)
  47. let PinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchFrom(recognizer:)))
  48. PinchGesture.delegate = self
  49. self.view?.addGestureRecognizer(PinchGesture)
  50.  
  51. let PanGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanFrom(recognizer:)))
  52. PanGesture.minimumNumberOfTouches = 2
  53. PanGesture.delegate = self
  54. self.view?.addGestureRecognizer(PanGesture)
  55. }
  56.  
  57. func setCameraConstraints() {
  58.  
  59. guard let camera = camera else { return }
  60.  
  61.  
  62. let scaledSize = CGSize(width: size.width * camera.xScale, height: size.height * camera.yScale)
  63.  
  64.  
  65. let boardContentRect = scene?.frame
  66.  
  67. let xInset = min((scaledSize.width / 2) - 0.0, (boardContentRect?.width)! / 2)
  68. let yInset = min((scaledSize.height / 2) - 0.0, (boardContentRect?.height)! / 2)
  69.  
  70. let insetContentRect = boardContentRect?.insetBy(dx: xInset, dy: yInset)
  71.  
  72. let xRange = SKRange(lowerLimit: (insetContentRect?.minX)!, upperLimit: (insetContentRect?.maxX)!)
  73. let yRange = SKRange(lowerLimit: (insetContentRect?.minY)!, upperLimit: (insetContentRect?.maxY)!)
  74.  
  75. let levelEdgeConstraint = SKConstraint.positionX(xRange, y: yRange)
  76. levelEdgeConstraint.referenceNode = scene
  77.  
  78. camera.constraints = [levelEdgeConstraint]
  79. }
  80.  
  81. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  82. return true
  83. }
  84. }
Add Comment
Please, Sign In to add comment