Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. //
  2. // DrawableView.swift
  3. // Drawing
  4. //
  5. // Created by Taichi Tsuchida on 2017/03/22.
  6. // Copyright © 2017 Taichi Tsuchida. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class DrawableView: UIView {
  12.  
  13. /*
  14. // Only override draw() if you perform custom drawing.
  15. // An empty implementation adversely affects performance during animation.
  16. override func draw(_ rect: CGRect) {
  17. // Drawing code
  18. }
  19. */
  20.  
  21. // override func draw(_ rect: CGRect) {
  22. //
  23. // }
  24.  
  25. class Line {
  26. var points: [CGPoint]
  27. var color :CGColor
  28. var width: CGFloat
  29.  
  30. init(color: CGColor, width: CGFloat){
  31. self.color = color
  32. self.width = width
  33. self.points = []
  34. }
  35.  
  36. func drawOnContext(context: CGContext){
  37. UIGraphicsPushContext(context)
  38.  
  39. context.setStrokeColor(self.color)
  40. context.setLineWidth(self.width)
  41. context.setLineCap(CGLineCap.round)
  42.  
  43. // 2点以上ないと線描画する必要なし
  44. if self.points.count > 1 {
  45. for (index, point) in self.points.enumerated() {
  46. if index == 0 {
  47. context.move(to: CGPoint(x:point.x, y:point.y))
  48. } else {
  49. context.addLine(to: CGPoint(x:point.x, y:point.y))
  50. }
  51. }
  52. }
  53. context.strokePath()
  54.  
  55. UIGraphicsPopContext()
  56. }
  57. }
  58.  
  59. var lines: [Line] = []
  60. var currentLine: Line? = nil
  61.  
  62. // タッチされた
  63. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  64. let point = touches.first!.location(in: self)
  65. currentLine = Line(color: UIColor.red.cgColor, width: 5)
  66. currentLine?.points.append(point)
  67. }
  68.  
  69. // タッチが動いた
  70. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  71. let point = touches.first!.location(in: self)
  72. currentLine?.points.append(point)
  73. self.setNeedsDisplay()
  74. }
  75.  
  76. // タッチが終わった
  77. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  78. // 2点以上のlineしか保存する必要なし
  79. if (currentLine?.points.count)! > 1 {
  80. lines.append(currentLine!)
  81. }
  82.  
  83. currentLine = nil
  84. self.setNeedsDisplay()
  85. }
  86.  
  87. func resetContext(context: CGContext) {
  88. context.clear(self.bounds)
  89. if let color = self.backgroundColor {
  90. color.setFill()
  91. } else {
  92. UIColor.white.setFill()
  93. }
  94. context.fill(self.bounds)
  95. }
  96.  
  97. //描画設定
  98. override func draw(_ rect: CGRect) {
  99.  
  100. let context = UIGraphicsGetCurrentContext()
  101.  
  102. //画面を一旦初期化
  103. resetContext(context: context!)
  104.  
  105. // 描き終わったline
  106. for line in lines {
  107. line.drawOnContext(context: context!)
  108. }
  109.  
  110. // 描いてる途中のline
  111. if let line = currentLine {
  112. line.drawOnContext(context: context!)
  113. }
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement