Class: ChaMenu
Instance Attribute Summary collapse
-
#background_color ⇒ Object
Returns the value of attribute background_color.
-
#data ⇒ Object
Returns the value of attribute data.
-
#delegate ⇒ Object
Returns the value of attribute delegate.
-
#font_size ⇒ Object
Returns the value of attribute font_size.
-
#highlight_color ⇒ Object
Returns the value of attribute highlight_color.
-
#normal_color ⇒ Object
Returns the value of attribute normal_color.
-
#scroll_view ⇒ Object
Returns the value of attribute scroll_view.
-
#selected_color ⇒ Object
Returns the value of attribute selected_color.
-
#selected_index ⇒ Object
Returns the value of attribute selected_index.
Instance Method Summary collapse
- #menu_button(text, index, frame) ⇒ Object
- #menu_select(sender) ⇒ Object
- #reload_data ⇒ Object
- #scrollViewDidScroll(scrollView) ⇒ Object
- #set_select_index(index) ⇒ Object
- #setup ⇒ Object
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_color ⇒ Object
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 |
#data ⇒ Object
Returns the value of attribute data.
2 3 4 |
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 2 def data @data end |
#delegate ⇒ Object
Returns the value of attribute delegate.
2 3 4 |
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 2 def delegate @delegate end |
#font_size ⇒ Object
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_color ⇒ Object
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_color ⇒ Object
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_view ⇒ Object
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_color ⇒ Object
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_index ⇒ Object
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
#menu_button(text, index, frame) ⇒ Object
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 (text, index, frame) = UIButton.alloc.initWithFrame(frame) .titleLabel.textAlignment = UITextAlignmentCenter .titleLabel.font = UIFont.systemFontOfSize(@font_size) .setTitle(text, forState:UIControlStateNormal) if index == @selected_index .setTitleColor(@selected_color, forState:UIControlStateNormal) else .setTitleColor(@normal_color, forState:UIControlStateNormal) end .setTitleColor(@highlight_color, forState:UIControlStateHighlighted) #button.setTitleColor(@selected_color, forState:UIControlStateSelected) .addTarget(self, action: 'menu_select:', forControlEvents:UIControlEventTouchUpInside) .tag = index end |
#menu_select(sender) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cha_work/ChaMenu/cha_menu.rb', line 71 def (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_index) else NSLog("Warning: Did not find selected_menu method.") end end |
#reload_data ⇒ Object
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 (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 |
#setup ⇒ Object
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 |