Class: ChaMenu

Inherits:
UIView show all
Defined in:
lib/cha_work/ChaMenu/cha_menu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from UIView

#<<, attr_updates, #hide, #off_gestures, #on_gesture, #on_pan, #on_pinch, #on_press, #on_press_begin, #on_rotate, #on_swipe, #on_tap, #show, #uiimage

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



4
5
6
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 4

def background_color
  @background_color
end

#dataObject

Returns the value of attribute data.



2
3
4
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 2

def data
  @data
end

#delegateObject

Returns the value of attribute delegate.



2
3
4
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 2

def delegate
  @delegate
end

#font_sizeObject

Returns the value of attribute font_size.



3
4
5
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 3

def font_size
  @font_size
end

#highlight_colorObject

Returns the value of attribute highlight_color.



3
4
5
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 3

def highlight_color
  @highlight_color
end

#normal_colorObject

Returns the value of attribute normal_color.



3
4
5
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 3

def normal_color
  @normal_color
end

#scroll_viewObject

Returns the value of attribute scroll_view.



2
3
4
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 2

def scroll_view
  @scroll_view
end

#selected_colorObject

Returns the value of attribute selected_color.



3
4
5
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 3

def selected_color
  @selected_color
end

#selected_indexObject

Returns the value of attribute selected_index.



2
3
4
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 2

def selected_index
  @selected_index
end

Instance Method Details



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 54

def menu_button(text, index, frame)
  button = UIButton.alloc.initWithFrame(frame)
  button.titleLabel.textAlignment = UITextAlignmentCenter
  button.titleLabel.font       = UIFont.systemFontOfSize(@font_size) 
  button.setTitle(text, forState:UIControlStateNormal)
  if index == @selected_index
    button.setTitleColor(@selected_color, forState:UIControlStateNormal)
  else
    button.setTitleColor(@normal_color, forState:UIControlStateNormal)
  end
  button.setTitleColor(@highlight_color, forState:UIControlStateHighlighted)
  #button.setTitleColor(@selected_color, forState:UIControlStateSelected)
  button.addTarget(self, action: 'menu_select:', forControlEvents:UIControlEventTouchUpInside)
  button.tag = index
  button
end


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 71

def menu_select(sender)
  UIView.animateWithDuration(0.2, delay:0, options:UIViewAnimationOptionCurveLinear, animations: -> {
    @scroll_view.scrollRectToVisibleCenteredOn(sender.frame, animated:false)
  }, completion:nil)

  @selected_index = sender.tag
  reload_data
  if @delegate.respond_to?(:selected_menu)
    @delegate.selected_menu(@selected_index)
  else
    NSLog("Warning: Did not find selected_menu method.")
  end
end

#reload_dataObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 30

def reload_data
    @scroll_view.subviews.each {|v| v.removeFromSuperview }
    
    x = 0
    padding = 10

    selected_frame = [[0,0], [0,0]]
    @data.each_with_index do |text, index|
      x += padding
      font   = UIFont.systemFontOfSize(@font_size)
      size   = text.sizeWithFont(font, constrainedToSize:[10000, 300], lineBreakMode:NSLineBreakByCharWrapping)
    frame   = [[x, padding], [size.width, size.height]]
    selected_frame = frame if index == @selected_index
    @scroll_view.addSubview menu_button(text, index, frame)
    x += size.width
  end

  content_width = x + padding
    @scroll_view.setContentSize([content_width, @scroll_view.frame.size.height])
    if @selected_index > 0
      @scroll_view.scrollRectToVisibleCenteredOn(selected_frame, animated:false)
    end
end

#scrollViewDidScroll(scrollView) ⇒ Object



89
90
91
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 89

def scrollViewDidScroll(scrollView)

end

#set_select_index(index) ⇒ Object



25
26
27
28
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 25

def set_select_index(index)
  return if @selected_index == index
  @selected_index = index
end

#setupObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 7

def setup
  self.autoresizingMask = UIViewAutoresizingFlexibleWidth
  @font_size ||= 16
  @normal_color  ||= UIColor.blackColor
  @highlight_color||= UIColor.grayColor
  @selected_color ||= UIColor.blueColor
  @selected_index ||= 0
  @scroll_view = UIScrollView.alloc.initWithFrame(self.bounds)
  @scroll_view.backgroundColor  = @background_color || UIColor.whiteColor
  @scroll_view.autoresizingMask = UIViewAutoresizingFlexibleWidth
  @scroll_view.showsVerticalScrollIndicator  = false
  @scroll_view.showsHorizontalScrollIndicator = false
  @scroll_view.delegate = self

  self.addSubview @scroll_view
  reload_data
end