Class: Motion::Xray::XrayToolbar

Inherits:
UIView
  • Object
show all
Defined in:
lib/motion-xray/views/xray_toolbar.rb

Overview

Toolbar is a terrible name. It’s more of a “tab bar”, but that name was taken. Plus, I dunno, maybe I’ll add other tools to it!

Direct Known Subclasses

PluginToolbar

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from UIView

build_xray, #xray, #xray_subviews

Instance Attribute Details

#canvasObject

Returns the value of attribute canvas.



6
7
8
# File 'lib/motion-xray/views/xray_toolbar.rb', line 6

def canvas
  @canvas
end

#delegateObject

Returns the value of attribute delegate.



8
9
10
# File 'lib/motion-xray/views/xray_toolbar.rb', line 8

def delegate
  @delegate
end

#selectedObject

Returns the value of attribute selected.



7
8
9
# File 'lib/motion-xray/views/xray_toolbar.rb', line 7

def selected
  @selected
end

Instance Method Details

#add(name, plugin_view) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/motion-xray/views/xray_toolbar.rb', line 53

def add(name, plugin_view)
  toolbar_item = XrayToolbarItem.alloc.initWithText(name)
  toolbar_item.frame = toolbar_item.frame.right(@item_x).down(5)
  @item_x += toolbar_item.frame.width
  @item_x += margin
  @scroll_view << toolbar_item
  @scroll_view.contentSize = [@item_x, self.bounds.height]
  @toolbar_items << toolbar_item
  @views << plugin_view

  unless @index
    select(0)
  end
end

#initWithFrame(frame) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/motion-xray/views/xray_toolbar.rb', line 10

def initWithFrame(frame)
  super.tap do
    self.backgroundColor = :white.uicolor
    self.layer.borderColor = :black.uicolor.cgcolor
    self.layer.borderWidth = 1
    grad_layer = CAGradientLayer.layer
    grad_layer.frame = self.layer.bounds
    grad_layer.colors = [0x526691.uicolor.cgcolor, 0x27386e.uicolor.cgcolor]
    self.layer << grad_layer

    @scroll_view = UIScrollView.alloc.initWithFrame(bounds)
    self << @scroll_view
    @selected_view = XraySelectedToolbarItem.new
    @scroll_view << @selected_view

    @index = nil
    @toolbar_items = []
    @views = []
    @item_x = margin

    recognizer = UITapGestureRecognizer.alloc.initWithTarget(self, action:'tapped:')
    recognizer.numberOfTapsRequired = 1
    recognizer.numberOfTouchesRequired = 1
    self.addGestureRecognizer(recognizer)
  end
end

#marginObject



49
50
51
# File 'lib/motion-xray/views/xray_toolbar.rb', line 49

def margin
  10
end

#select(index) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/motion-xray/views/xray_toolbar.rb', line 68

def select(index)
  toolbar_item = @toolbar_items[index]
  will_hide
  @index = index
  self.canvas.subviews.each &:removeFromSuperview
  UIView.animate {
    @selected_view.item = toolbar_item
  }
  will_show

  plugin_view = @views[@index]
  self.canvas << plugin_view

  if self.canvas.is_a?(UIScrollView)
    self.canvas.contentSize = plugin_view.frame.size
    self.canvas.setContentOffset([0, 0], animated: false)
  end
end

#showObject



87
88
89
# File 'lib/motion-xray/views/xray_toolbar.rb', line 87

def show
  will_show
end

#tapped(event) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/motion-xray/views/xray_toolbar.rb', line 37

def tapped(event)
  touched_x = event.locationInView(self).x + @scroll_view.contentOffset.x
  touched_index = nil
  @toolbar_items.each_with_index do |item, index|
    if touched_x < item.frame.max_x
      touched_index = index
      break
    end
  end
  select(touched_index) if touched_index
end

#will_hideObject



91
92
# File 'lib/motion-xray/views/xray_toolbar.rb', line 91

def will_hide
end

#will_showObject



94
95
# File 'lib/motion-xray/views/xray_toolbar.rb', line 94

def will_show
end