accordion_view 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'bubble-wrap'
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Amit Kumar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # AccordionView
2
+
3
+ A RubyMotion gem to create AccordionView
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'accordion_view'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install accordion_view
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,21 @@
1
+ $:.unshift("/Library/RubyMotion/lib")
2
+ require 'motion/project'
3
+ require "bundler/gem_tasks"
4
+ require "bundler/setup"
5
+
6
+ $:.unshift("./lib/")
7
+ require './lib/accordion_view'
8
+
9
+ unless defined?(Motion::Project::Config)
10
+ raise "This file must be required within a RubyMotion project Rakefile."
11
+ end
12
+
13
+ Motion::Project::App.setup do |app|
14
+ # Use `rake config' to see complete project settings.
15
+ app.name = 'AccordionView'
16
+
17
+ # Temporary, until RubyMotion patches its test loader
18
+ # spec_files = app.spec_files + Dir.glob(File.join(app.specs_dir, '**/*.rb'))
19
+ # spec_files.uniq!
20
+ # app.instance_variable_set(:@spec_files, spec_files)
21
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/accordion_view/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Amit Kumar"]
6
+ gem.email = ["toamitkumar@gmail.com"]
7
+ gem.description = "Adding Accordion to your view"
8
+ gem.summary = "Adding Accordion to your view"
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+
14
+ gem.name = "accordion_view"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = AccordionView::VERSION
17
+
18
+ gem.add_dependency "bubble-wrap"
19
+ gem.add_development_dependency 'rake'
20
+ end
@@ -0,0 +1,7 @@
1
+ require "accordion_view/version"
2
+
3
+ Motion::Project::App.setup do |app|
4
+ Dir.glob(File.join(File.dirname(__FILE__), 'accordion_view/*.rb')).each do |file|
5
+ app.files.unshift(file)
6
+ end
7
+ end
@@ -0,0 +1,222 @@
1
+ class Accordion < UIView
2
+
3
+ attr_accessor :views, :headers, :original_sizes, :scroll_view
4
+
5
+ attr_accessor :selected_index
6
+
7
+ attr_accessor :animation_duration
8
+
9
+ attr_accessor :animation_curve
10
+
11
+ attr_accessor :allow_multiple_selection
12
+
13
+ # Set of selected inddxes for multiple selection
14
+ attr_accessor :selection_indexes
15
+
16
+ attr_accessor :delegate
17
+
18
+
19
+ def initWithFrame(frame)
20
+
21
+ if(super)
22
+ @views = []
23
+ @headers = []
24
+ @original_sizes = []
25
+ self.backgroundColor = UIColor.clearColor
26
+ self.userInteractionEnabled = true
27
+ @animation_duration = 0.3
28
+ @animation_curve = UIViewAnimationCurveEaseIn
29
+ self.autoresizesSubviews = false
30
+ @selection_indexes = NSIndexSet.new
31
+
32
+ @scroll_view = UIScrollView.alloc.initWithFrame([[0, 0], [self.frame.size.width, self.frame.size.height]])
33
+ @scroll_view.backgroundColor = UIColor.clearColor
34
+ @scroll_view.userInteractionEnabled = true
35
+ @scroll_view.autoresizesSubviews = false
36
+ @scroll_view.scrollsToTop = false
37
+ @scroll_view.delegate = self
38
+
39
+ addSubview(@scroll_view)
40
+
41
+ @allow_multiple_selection = false
42
+
43
+ end
44
+
45
+ self
46
+ end
47
+
48
+
49
+ def add_header(header, withView:view)
50
+ if(header and view)
51
+
52
+ @headers << header
53
+ @views << view
54
+ @original_sizes << NSValue.valueWithCGSize(view.frame.size)
55
+ view.setAutoresizingMask(UIViewAutoresizingNone)
56
+ view.setClipsToBounds(true)
57
+
58
+ # modify the width of the header to the width of accordion
59
+ frame = header.frame
60
+ frame.origin.x = 0
61
+ frame.size.width = self.frame.size.width
62
+ header.setFrame(frame)
63
+ # header.rounded_corners
64
+
65
+ # modify the width of the body to the width of accordion
66
+ frame = view.frame
67
+ frame.origin.x = 0
68
+ frame.size.width = self.frame.size.width
69
+ view.setFrame(frame)
70
+ # view.rounded_corners
71
+
72
+
73
+ @scroll_view.addSubview(header)
74
+ @scroll_view.addSubview(view)
75
+
76
+ if(header.respondsToSelector("addTarget:action:forControlEvents:"))
77
+ header.setTag(@headers.size - 1)
78
+ header.addTarget(self, action:"touch_down:", forControlEvents:UIControlEventTouchUpInside)
79
+ end
80
+
81
+ set_selected_index(0) if(@selection_indexes.count == 0)
82
+
83
+ end
84
+ end
85
+
86
+ def set_selection_indexes(_selection_indexes)
87
+ return if(@headers.size == 0)
88
+
89
+ if(not @allow_multiple_selection and _selection_indexes.count > 1)
90
+ _selection_indexes = NSIndexSet.indexSetWithIndex(_selection_indexes.firstIndex)
91
+ end
92
+
93
+ clean_indexes = NSMutableIndexSet.new
94
+ _selection_indexes.enumerateIndexesUsingBlock(lambda do |index, stop|
95
+ return if(index > @headers.size-1)
96
+
97
+ clean_indexes.addIndex(index)
98
+ end)
99
+
100
+ @selection_indexes = clean_indexes
101
+ self.setNeedsLayout
102
+
103
+ if(@delegate.respondsToSelector("accordion:didChangeSelection:"))
104
+ @delegate.accordion(self, didChangeSelection:@selection_indexes)
105
+ end
106
+
107
+ end
108
+
109
+ def set_selected_index(index)
110
+ set_selection_indexes(NSIndexSet.indexSetWithIndex(index))
111
+ end
112
+
113
+ def selected_index
114
+ @selection_indexes.first
115
+ end
116
+
117
+ def set_original_size(size, forIndex:index)
118
+ return if(index >= @views.size)
119
+
120
+ @original_sizes[index] = NSValue.valueWithCGSize(size)
121
+
122
+ self.setNeedsLayout if(@selection_indexes.containsIndex(index))
123
+ end
124
+
125
+ def touch_down(sender)
126
+ if(@allow_multiple_selection)
127
+ temp_copy = @selection_indexes.mutableCopy
128
+
129
+ if(@selection_indexes.containsIndex(sender.tag))
130
+ temp_copy.removeIndex(sender.tag)
131
+ else
132
+ temp_copy.addIndex(sender.tag)
133
+ end
134
+ set_selection_indexes(temp_copy)
135
+ else
136
+ set_selected_index(sender.tag)
137
+ end
138
+ end
139
+
140
+ def animation_done
141
+ @views.each_with_index do |view, index|
142
+ view.setHidden(true) unless(@selection_indexes.containsIndex(index))
143
+ end
144
+ end
145
+
146
+ def layoutSubviews
147
+ height = 0
148
+ @views.each_with_index do |view, index|
149
+ original_size = @original_sizes[index].CGSizeValue
150
+ view_frame = @views[index].frame
151
+ header_frame = @headers[index].frame
152
+ header_frame.origin.y = height
153
+ height += header_frame.size.height
154
+ view_frame.origin.y = height
155
+
156
+ if(@selection_indexes.containsIndex(index))
157
+ view_frame.size.height = original_size.height
158
+ @views[index].setFrame(CGRectMake(0, view_frame.origin.y, self.frame.size.width, 0))
159
+ @views[index].setHidden(false)
160
+ else
161
+ view_frame.size.height = 0
162
+ end
163
+
164
+ height += view_frame.size.height
165
+
166
+ if(not CGRectEqualToRect(@views[index].frame, view_frame) or not CGRectEqualToRect(@headers[index].frame, header_frame))
167
+ UIView.beginAnimations(nil, context:nil)
168
+ UIView.setAnimationDelegate(self)
169
+ UIView.setAnimationDidStopSelector("animation_done:")
170
+ UIView.setAnimationDuration(@animation_duration)
171
+ UIView.setAnimationCurve(@animation_curve)
172
+ UIView.setAnimationBeginsFromCurrentState(true)
173
+ @headers[index].setFrame(header_frame)
174
+ @views[index].setFrame(view_frame)
175
+ UIView.commitAnimations
176
+ end
177
+
178
+ offset = @scroll_view.contentOffset
179
+
180
+ UIView.beginAnimations(nil, context:nil)
181
+ UIView.setAnimationDuration(@animation_duration)
182
+ UIView.setAnimationCurve(@animation_curve)
183
+ UIView.setAnimationBeginsFromCurrentState(true)
184
+ @scroll_view.setContentSize(CGSizeMake(self.frame.size.width, height))
185
+ UIView.commitAnimations
186
+
187
+ if(offset.y + @scroll_view.size.height > height)
188
+ offset.y = height - @scroll_view.frame.size.height
189
+ offset.y = 0 if(offset.y < 0)
190
+ end
191
+
192
+ @scroll_view.setContentOffset(offset, animated:true)
193
+ self.scrollViewDidScroll(@scroll_view)
194
+ end
195
+ end
196
+
197
+ def scrollViewDidScroll(_scroll_view)
198
+ @views.each_with_index do |view, index|
199
+ if(view.frame.size.height > 0)
200
+ header = @headers[index]
201
+ content = view.frame
202
+
203
+ content.origin.y -= header.frame.size.height
204
+ content.size.height += header.frame.size.height
205
+
206
+ frame = header.frame
207
+
208
+ if (CGRectContainsPoint(content, _scroll_view.contentOffset))
209
+ if (_scroll_view.contentOffset.y < content.origin.y + content.size.height - frame.size.height)
210
+ frame.origin.y = _scroll_view.contentOffset.y
211
+ else
212
+ frame.origin.y = content.origin.y + content.size.height - frame.size.height
213
+ end
214
+ else
215
+ frame.origin.y = view.frame.origin.y - frame.size.height
216
+ end
217
+ header.frame = frame
218
+ end
219
+ end
220
+ end
221
+
222
+ end
@@ -0,0 +1,3 @@
1
+ module AccordionView
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accordion_view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Amit Kumar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bubble-wrap
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Adding Accordion to your view
47
+ email:
48
+ - toamitkumar@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - accordion_view.gemspec
59
+ - lib/accordion_view.rb
60
+ - lib/accordion_view/accordion.rb
61
+ - lib/accordion_view/version.rb
62
+ homepage: ''
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.24
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Adding Accordion to your view
86
+ test_files: []