Class: GridOverlayView

Inherits:
UIView
  • Object
show all
Defined in:
lib/project/views/overlay_view.rb

Constant Summary collapse

DEFAULTS =
{ stroke_color: UIColor.grayColor,
stroke_width: 1.0,
x_lines: 3,
y_lines: 3 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stroke_colorObject

Returns the value of attribute stroke_color.



8
9
10
# File 'lib/project/views/overlay_view.rb', line 8

def stroke_color
  @stroke_color
end

#stroke_widthObject

Returns the value of attribute stroke_width.



8
9
10
# File 'lib/project/views/overlay_view.rb', line 8

def stroke_width
  @stroke_width
end

#x_intervalObject

Returns the value of attribute x_interval.



7
8
9
# File 'lib/project/views/overlay_view.rb', line 7

def x_interval
  @x_interval
end

#y_intervalObject

Returns the value of attribute y_interval.



7
8
9
# File 'lib/project/views/overlay_view.rb', line 7

def y_interval
  @y_interval
end

Instance Method Details

#contextObject



109
110
111
# File 'lib/project/views/overlay_view.rb', line 109

def context
  @_context ||= UIGraphicsGetCurrentContext()
end

#draw_horizontal_linesObject



93
94
95
96
# File 'lib/project/views/overlay_view.rb', line 93

def draw_horizontal_lines
  draw_line([0, y_interval],     [size.width, y_interval])
  draw_line([0, y_interval * 2], [size.width, y_interval * 2])
end

#draw_line(start_point, end_point) ⇒ Object



103
104
105
106
107
# File 'lib/project/views/overlay_view.rb', line 103

def draw_line(start_point, end_point)
  CGContextMoveToPoint(context, start_point[0], start_point[1])

  CGContextAddLineToPoint(context, end_point[0], end_point[1])
end

#draw_vertical_linesObject



98
99
100
101
# File 'lib/project/views/overlay_view.rb', line 98

def draw_vertical_lines
  draw_line([x_interval, 0],     [x_interval, size.height])
  draw_line([x_interval * 2, 0], [x_interval * 2.0, size.height])
end

#drawRect(rect) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/project/views/overlay_view.rb', line 79

def drawRect(rect)
  super

  setup_drawing
  draw_vertical_lines
  draw_horizontal_lines

  fill_path
end

#fill_pathObject



89
90
91
# File 'lib/project/views/overlay_view.rb', line 89

def fill_path
  CGContextStrokePath(context)
end

#hidden?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/project/views/overlay_view.rb', line 21

def hidden?
  stroke_color == UIColor.clearColor
end

#hideObject



39
40
41
42
43
44
45
46
47
# File 'lib/project/views/overlay_view.rb', line 39

def hide
  unless hidden?
    @original_color = stroke_color

    self.stroke_color = UIColor.clearColor

    setNeedsDisplay
  end
end

#initWithFrame(frame) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/project/views/overlay_view.rb', line 10

def initWithFrame(frame)
  super.tap do |view|
    view.backgroundColor = UIColor.clearColor

    self.x_lines      = DEFAULTS[:x_lines]
    self.y_lines      = DEFAULTS[:y_lines]
    self.stroke_color = DEFAULTS[:stroke_color]
    self.stroke_width = DEFAULTS[:stroke_width]
  end
end

#setup_drawingObject



73
74
75
76
77
# File 'lib/project/views/overlay_view.rb', line 73

def setup_drawing
  CGContextSetStrokeColorWithColor(context, stroke_color.CGColor)

  CGContextSetLineWidth(context, stroke_width)
end

#showObject



33
34
35
36
37
# File 'lib/project/views/overlay_view.rb', line 33

def show
  self.stroke_color = @original_color

  setNeedsDisplay
end

#toggleObject



25
26
27
28
29
30
31
# File 'lib/project/views/overlay_view.rb', line 25

def toggle
  if hidden?
    show
  else
    hide
  end
end

#x_lines=(lines) ⇒ Object



49
50
51
52
53
# File 'lib/project/views/overlay_view.rb', line 49

def x_lines=(lines)
  self.x_interval = frame.size.width / lines

  setNeedsDisplay
end

#y_lines=(lines) ⇒ Object



55
56
57
58
59
# File 'lib/project/views/overlay_view.rb', line 55

def y_lines=(lines)
  self.y_interval = frame.size.height / lines

  setNeedsDisplay
end