Shrooms

Untitled

Apr 14th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. //
  2. // GameScreen.swift
  3. // LineRider
  4. //
  5. // Created by Andrew Solesa on 2017-04-14.
  6. // Copyright © 2017 KSG. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class GameScreen: UIScrollView
  12. {
  13. // LINE VARIABLES
  14.  
  15. var lastPoint = CGPoint.zero
  16. var red: CGFloat = 0.0
  17. var green: CGFloat = 0.0
  18. var blue: CGFloat = 0.0
  19. var brushWidth: CGFloat = 10.0
  20. var opacity: CGFloat = 1.0
  21. var swiped = false
  22.  
  23. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
  24. {
  25. print("TOUCHED")
  26. swiped = false
  27.  
  28. if let touch = touches.first
  29. {
  30. lastPoint = touch.location(in: self)
  31. }
  32. }
  33.  
  34. func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint)
  35. {
  36. UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0)
  37.  
  38. let context = UIGraphicsGetCurrentContext()
  39.  
  40. context?.move(to: fromPoint)
  41. context?.addLine(to: toPoint)
  42.  
  43. context?.setLineCap(CGLineCap.round)
  44. context?.setLineWidth(brushWidth)
  45. context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
  46. context?.setBlendMode(CGBlendMode.normal)
  47. context?.strokePath()
  48.  
  49. UIGraphicsEndImageContext()
  50. }
  51.  
  52. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
  53. {
  54. print("MOVED")
  55. swiped = true
  56.  
  57. if let touch = touches.first
  58. {
  59. print("CREATING POINT")
  60. let currentPoint = touch.location(in: self)
  61. drawLine(from: lastPoint, to: currentPoint)
  62.  
  63. lastPoint = currentPoint
  64. }
  65. }
  66.  
  67. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
  68. {
  69. if !swiped
  70. {
  71. self.drawLine(from: lastPoint, to: lastPoint)
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment