Class: Accordion
- Inherits:
-
UIView
- Object
- UIView
- Accordion
- Defined in:
- lib/accordion_view/accordion.rb
Instance Attribute Summary collapse
-
#allow_multiple_selection ⇒ Object
Returns the value of attribute allow_multiple_selection.
-
#animation_curve ⇒ Object
Returns the value of attribute animation_curve.
-
#animation_duration ⇒ Object
Returns the value of attribute animation_duration.
-
#delegate ⇒ Object
Returns the value of attribute delegate.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#original_sizes ⇒ Object
Returns the value of attribute original_sizes.
-
#scroll_view ⇒ Object
Returns the value of attribute scroll_view.
-
#selected_index ⇒ Object
Returns the value of attribute selected_index.
-
#selection_indexes ⇒ Object
Set of selected inddxes for multiple selection.
-
#views ⇒ Object
Returns the value of attribute views.
Instance Method Summary collapse
- #add_header(header, withView: view) ⇒ Object
- #animation_done ⇒ Object
- #initWithFrame(frame) ⇒ Object
- #layoutSubviews ⇒ Object
- #scrollViewDidScroll(_scroll_view) ⇒ Object
- #set_original_size(size, forIndex: index) ⇒ Object
- #set_selected_index(index) ⇒ Object
- #set_selection_indexes(_selection_indexes) ⇒ Object
- #touch_down(sender) ⇒ Object
Instance Attribute Details
#allow_multiple_selection ⇒ Object
Returns the value of attribute allow_multiple_selection.
11 12 13 |
# File 'lib/accordion_view/accordion.rb', line 11 def allow_multiple_selection @allow_multiple_selection end |
#animation_curve ⇒ Object
Returns the value of attribute animation_curve.
9 10 11 |
# File 'lib/accordion_view/accordion.rb', line 9 def animation_curve @animation_curve end |
#animation_duration ⇒ Object
Returns the value of attribute animation_duration.
7 8 9 |
# File 'lib/accordion_view/accordion.rb', line 7 def animation_duration @animation_duration end |
#delegate ⇒ Object
Returns the value of attribute delegate.
16 17 18 |
# File 'lib/accordion_view/accordion.rb', line 16 def delegate @delegate end |
#headers ⇒ Object
Returns the value of attribute headers.
3 4 5 |
# File 'lib/accordion_view/accordion.rb', line 3 def headers @headers end |
#original_sizes ⇒ Object
Returns the value of attribute original_sizes.
3 4 5 |
# File 'lib/accordion_view/accordion.rb', line 3 def original_sizes @original_sizes end |
#scroll_view ⇒ Object
Returns the value of attribute scroll_view.
3 4 5 |
# File 'lib/accordion_view/accordion.rb', line 3 def scroll_view @scroll_view end |
#selected_index ⇒ Object
Returns the value of attribute selected_index.
5 6 7 |
# File 'lib/accordion_view/accordion.rb', line 5 def selected_index @selected_index end |
#selection_indexes ⇒ Object
Set of selected inddxes for multiple selection
14 15 16 |
# File 'lib/accordion_view/accordion.rb', line 14 def selection_indexes @selection_indexes end |
#views ⇒ Object
Returns the value of attribute views.
3 4 5 |
# File 'lib/accordion_view/accordion.rb', line 3 def views @views end |
Instance Method Details
#add_header(header, withView: view) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/accordion_view/accordion.rb', line 49 def add_header(header, withView:view) if(header and view) @headers << header @views << view @original_sizes << NSValue.valueWithCGSize(view.frame.size) view.setAutoresizingMask(UIViewAutoresizingNone) view.setClipsToBounds(true) # modify the width of the header to the width of accordion frame = header.frame frame.origin.x = 0 frame.size.width = self.frame.size.width header.setFrame(frame) # header.rounded_corners # modify the width of the body to the width of accordion frame = view.frame frame.origin.x = 0 frame.size.width = self.frame.size.width view.setFrame(frame) # view.rounded_corners @scroll_view.addSubview(header) @scroll_view.addSubview(view) if(header.respondsToSelector("addTarget:action:forControlEvents:")) header.setTag(@headers.size - 1) header.addTarget(self, action:"touch_down:", forControlEvents:UIControlEventTouchUpInside) end set_selected_index(0) if(@selection_indexes.count == 0) end end |
#animation_done ⇒ Object
140 141 142 143 144 |
# File 'lib/accordion_view/accordion.rb', line 140 def animation_done @views.each_with_index do |view, index| view.setHidden(true) unless(@selection_indexes.containsIndex(index)) end end |
#initWithFrame(frame) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/accordion_view/accordion.rb', line 19 def initWithFrame(frame) if(super) @views = [] @headers = [] @original_sizes = [] self.backgroundColor = UIColor.clearColor self.userInteractionEnabled = true @animation_duration = 0.3 @animation_curve = UIViewAnimationCurveEaseIn self.autoresizesSubviews = false @selection_indexes = NSIndexSet.new @scroll_view = UIScrollView.alloc.initWithFrame([[0, 0], [self.frame.size.width, self.frame.size.height]]) @scroll_view.backgroundColor = UIColor.clearColor @scroll_view.userInteractionEnabled = true @scroll_view.autoresizesSubviews = false @scroll_view.scrollsToTop = false @scroll_view.delegate = self addSubview(@scroll_view) @allow_multiple_selection = false end self end |
#layoutSubviews ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/accordion_view/accordion.rb', line 146 def layoutSubviews height = 0 @views.each_with_index do |view, index| original_size = @original_sizes[index].CGSizeValue view_frame = @views[index].frame header_frame = @headers[index].frame header_frame.origin.y = height height += header_frame.size.height view_frame.origin.y = height if(@selection_indexes.containsIndex(index)) view_frame.size.height = original_size.height @views[index].setFrame(CGRectMake(0, view_frame.origin.y, self.frame.size.width, 0)) @views[index].setHidden(false) else view_frame.size.height = 0 end height += view_frame.size.height if(not CGRectEqualToRect(@views[index].frame, view_frame) or not CGRectEqualToRect(@headers[index].frame, header_frame)) UIView.beginAnimations(nil, context:nil) UIView.setAnimationDelegate(self) UIView.setAnimationDidStopSelector("animation_done:") UIView.setAnimationDuration(@animation_duration) UIView.setAnimationCurve(@animation_curve) UIView.setAnimationBeginsFromCurrentState(true) @headers[index].setFrame(header_frame) @views[index].setFrame(view_frame) UIView.commitAnimations end offset = @scroll_view.contentOffset UIView.beginAnimations(nil, context:nil) UIView.setAnimationDuration(@animation_duration) UIView.setAnimationCurve(@animation_curve) UIView.setAnimationBeginsFromCurrentState(true) @scroll_view.setContentSize(CGSizeMake(self.frame.size.width, height)) UIView.commitAnimations if(offset.y + @scroll_view.size.height > height) offset.y = height - @scroll_view.frame.size.height offset.y = 0 if(offset.y < 0) end @scroll_view.setContentOffset(offset, animated:true) self.scrollViewDidScroll(@scroll_view) end end |
#scrollViewDidScroll(_scroll_view) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/accordion_view/accordion.rb', line 197 def scrollViewDidScroll(_scroll_view) @views.each_with_index do |view, index| if(view.frame.size.height > 0) header = @headers[index] content = view.frame content.origin.y -= header.frame.size.height content.size.height += header.frame.size.height frame = header.frame if (CGRectContainsPoint(content, _scroll_view.contentOffset)) if (_scroll_view.contentOffset.y < content.origin.y + content.size.height - frame.size.height) frame.origin.y = _scroll_view.contentOffset.y else frame.origin.y = content.origin.y + content.size.height - frame.size.height end else frame.origin.y = view.frame.origin.y - frame.size.height end header.frame = frame end end end |
#set_original_size(size, forIndex: index) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/accordion_view/accordion.rb', line 117 def set_original_size(size, forIndex:index) return if(index >= @views.size) @original_sizes[index] = NSValue.valueWithCGSize(size) self.setNeedsLayout if(@selection_indexes.containsIndex(index)) end |
#set_selected_index(index) ⇒ Object
109 110 111 |
# File 'lib/accordion_view/accordion.rb', line 109 def set_selected_index(index) set_selection_indexes(NSIndexSet.indexSetWithIndex(index)) end |
#set_selection_indexes(_selection_indexes) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/accordion_view/accordion.rb', line 86 def set_selection_indexes(_selection_indexes) return if(@headers.size == 0) if(not @allow_multiple_selection and _selection_indexes.count > 1) _selection_indexes = NSIndexSet.indexSetWithIndex(_selection_indexes.firstIndex) end clean_indexes = NSMutableIndexSet.new _selection_indexes.enumerateIndexesUsingBlock(lambda do |index, stop| return if(index > @headers.size-1) clean_indexes.addIndex(index) end) @selection_indexes = clean_indexes self.setNeedsLayout if(@delegate.respondsToSelector("accordion:didChangeSelection:")) @delegate.accordion(self, didChangeSelection:@selection_indexes) end end |
#touch_down(sender) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/accordion_view/accordion.rb', line 125 def touch_down(sender) if(@allow_multiple_selection) temp_copy = @selection_indexes.mutableCopy if(@selection_indexes.containsIndex(sender.tag)) temp_copy.removeIndex(sender.tag) else temp_copy.addIndex(sender.tag) end set_selection_indexes(temp_copy) else set_selected_index(sender.tag) end end |