Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  2. let pos = touches.first!.location(in: view)
  3. let center = view!.center
  4. let xDist = pos.x - center.x
  5. let yDist = pos.y - center.y
  6. let distance = sqrt((xDist * xDist) + (yDist * yDist)) * 0.02
  7. camera?.position.x += xDist * distance * 0.02
  8. camera?.position.y -= yDist * distance * 0.02
  9. }
  10.  
  11. private var difference = CGVector()
  12.  
  13. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  14. let pos = touches.first!.location(in: view)
  15. let center = view!.center
  16. difference = CGVector(dx: pos.x - center.x, dy: pos.y - center.y)
  17. }
  18.  
  19. override func update(_ currentTime: TimeInterval) {
  20. let distance = sqrt((difference.dx * difference.dx) + (difference.dy * difference.dy)) * 0.02
  21. camera?.position.x += difference.dx * distance * 0.02
  22. camera?.position.y -= difference.dy * distance * 0.02
  23. }
  24.  
  25. private var difference = CGVector()
  26. private var touching: Bool = false
  27. private var fade: CGFloat = 0
  28.  
  29. private func touched(_ touch: UITouch) {
  30. let pos = touch.location(in: view)
  31. let center = view!.center
  32. difference = CGVector(dx: pos.x - center.x, dy: pos.y - center.y)
  33. }
  34.  
  35. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  36. touching = true
  37. touched(touches.first!)
  38. }
  39.  
  40. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  41. touched(touches.first!)
  42. }
  43.  
  44. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  45. touching = false
  46. touched(touches.first!)
  47. }
  48.  
  49. override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
  50. touching = false
  51. }
  52.  
  53. override func update(_ currentTime: TimeInterval) {
  54. if touching {
  55. if fade < 0.02 { fade += 0.0005 }
  56. } else {
  57. if fade > 0 { fade -= 0.0005 }
  58. }
  59. let distance = sqrt((difference.dx * difference.dx) + (difference.dy * difference.dy)) * 0.01
  60. camera?.position.x += difference.dx * distance * fade
  61. camera?.position.y -= difference.dy * distance * fade
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement