swipe_title 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in swipe_title.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 DmitryTsepelev
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SwipeTitle
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'swipe_title'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install swipe_title
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 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
@@ -0,0 +1,97 @@
1
+ class SwipeCollectionViewController < UICollectionViewController
2
+
3
+ CELL_ID = 'CellID'
4
+
5
+ attr_accessor :delegate
6
+ attr_accessor :selectedIndex
7
+
8
+ def initWithFrame frame
9
+ initWithCollectionViewLayout SwipeTitleLayout.new
10
+
11
+ collectionView.frame = CGRectMake(frame.origin.x,
12
+ frame.origin.y,
13
+ frame.size.width,
14
+ frame.size.height)
15
+
16
+ self
17
+ end
18
+
19
+ def initWithCollectionViewLayout layout
20
+ super
21
+
22
+ collectionView.backgroundColor = UIColor.clearColor
23
+ collectionView.showsHorizontalScrollIndicator = false
24
+
25
+ selectedIndex = 0
26
+
27
+ @layout = layout
28
+ @layout.delegate = self
29
+
30
+ @titles = []
31
+
32
+ collectionView.registerClass TitlePage,
33
+ forCellWithReuseIdentifier: CELL_ID
34
+
35
+ self
36
+ end
37
+
38
+ def titles= titles
39
+ @titles = titles
40
+ reloadData
41
+ end
42
+
43
+ def scrollToItemAtIndex index
44
+ return if index > @titles.length - 1
45
+
46
+ self.selectedIndex = index
47
+
48
+ p = CGPointMake(@layout.centerOfElementAtIndex(index), 0)
49
+ collectionView.setContentOffset p, animated: true
50
+ end
51
+
52
+ def selectedIndex= index
53
+ delegate.swipeTitleControllerSelectedIndex(self, index)
54
+ end
55
+
56
+ def reloadData
57
+ collectionView.reloadData
58
+ end
59
+
60
+ def collectionView view, numberOfItemsInSection: section
61
+ @titles.length
62
+ end
63
+
64
+ def didSelectRowAtIndex index
65
+ delegate.didSelectRowAtIndex index if delegate.responds_to?(:didSelectRowAtIndex)
66
+ end
67
+
68
+ def collectionView view, cellForItemAtIndexPath: indexPath
69
+ view.dequeueReusableCellWithReuseIdentifier(CELL_ID,
70
+ forIndexPath: indexPath).tap do |c|
71
+ c.titleLabel.text = @titles[indexPath.row]
72
+ c.titleLabel.color = UIColor.whiteColor
73
+ c.titleLabel.shadowColor = UIColor["#006b62"]
74
+ c.titleLabel.shadowOffset = [1, 1]
75
+ end
76
+ end
77
+
78
+ def scrollViewWillBeginDragging scrollView
79
+ delegate.swipeBegan
80
+ end
81
+
82
+ def scrollViewWillEndDragging scrollView,
83
+ withVelocity: velocity,
84
+ targetContentOffset: targetContentOffset
85
+ delegate.swipeEnded
86
+ end
87
+
88
+ def scrollViewDidScroll scrollView
89
+ offset = scrollView.contentOffset
90
+
91
+ offset.x = 0 if offset.x < 0
92
+ offset.x = scrollView.contentSize.width - scrollView.frame.size.width if offset.x + scrollView.frame.size.width > scrollView.contentSize.width
93
+
94
+ scrollView.contentOffset = offset
95
+ end
96
+
97
+ end
@@ -0,0 +1,68 @@
1
+ class SwipeTitleController < UIViewController
2
+
3
+ CELL_ID = 'CellID'
4
+
5
+ attr_accessor :delegate
6
+ attr_accessor :selectedIndex
7
+
8
+ def viewDidLoad
9
+ view.layer.tap do |l|
10
+ l.masksToBounds = false
11
+ l.cornerRadius = 5
12
+ l.shadowOffset = CGSizeMake(0, 0)
13
+ l.shadowRadius = 8
14
+ l.shadowOpacity = 0
15
+ end
16
+
17
+ @swipeController = SwipeCollectionViewController.alloc.initWithFrame(CGRectZero)
18
+ @swipeController.delegate = self
19
+ view.addSubview @swipeController.collectionView
20
+
21
+ @pageControl = UIPageControl.alloc.initWithFrame(CGRectZero)
22
+ @pageControl.userInteractionEnabled = false
23
+ view.addSubview @pageControl
24
+ end
25
+
26
+ def viewWillLayoutSubviews
27
+ @swipeController.collectionView.frame = CGRectMake(0, 0,
28
+ view.frame.size.width, view.frame.size.height - 14)
29
+ @pageControl.frame = CGRectMake(0, view.frame.size.height - 14,
30
+ view.frame.size.width, 14)
31
+ end
32
+
33
+ def scrollToItemAtIndex index
34
+ @swipeController.scrollToItemAtIndex index
35
+ @pageControl.currentPage = index
36
+ end
37
+
38
+ def swipeTitleControllerSelectedIndex controller, index
39
+ @pageControl.currentPage = index
40
+ delegate.swipeTitleControllerSelectedIndex controller, index
41
+ end
42
+
43
+ def titles= titles
44
+ @pageControl.numberOfPages = titles.length
45
+
46
+ @pageControl.hidden = titles.length == 1
47
+
48
+ @swipeController.titles = titles
49
+ @swipeController.reloadData
50
+ end
51
+
52
+ # Called by Swipe Collection View Controller
53
+
54
+ def swipeBegan
55
+ view.layer.shadowOpacity = 1
56
+ end
57
+
58
+ def swipeEnded
59
+ UIView.transitionWithView view,
60
+ duration: 0.1,
61
+ options: UIViewAnimationOptionTransitionCrossDissolve,
62
+ animations: -> {
63
+ view.layer.shadowOpacity = 0
64
+ },
65
+ completion:nil
66
+ end
67
+
68
+ end
@@ -0,0 +1,103 @@
1
+ class SwipeTitleLayout < UICollectionViewLayout
2
+
3
+ attr_accessor :delegate
4
+
5
+ def rows
6
+ @rows
7
+ end
8
+
9
+ def init
10
+ super
11
+ @centerPostions = {}
12
+ self
13
+ end
14
+
15
+ def shouldInvalidateLayoutForBoundsChange oldBounds
16
+ true
17
+ end
18
+
19
+ def prepareLayout
20
+ super
21
+
22
+ @titles = if collectionView.numberOfSections > 0
23
+ collectionView.numberOfItemsInSection 0
24
+ else
25
+ 0
26
+ end
27
+ end
28
+
29
+ def collectionViewContentSize
30
+ width = @titles * collectionView.frame.size.width
31
+ size = CGSizeMake(width, collectionView.frame.size.height)
32
+ collectionView.contentSize = size
33
+ size
34
+ end
35
+
36
+ def layoutAttributesForElementsInRect rect
37
+ objs = []
38
+ @titles.times do |t|
39
+ indexPath = NSIndexPath.indexPathForItem(t, inSection:0)
40
+ objs << layoutAttributesForItemAtIndexPath(indexPath)
41
+ end
42
+
43
+ objs
44
+ end
45
+
46
+ def scrollViewDidScroll pos
47
+ index = selectElementAtPosition pos
48
+ end
49
+
50
+ def layoutAttributesForItemAtIndexPath path
51
+ UICollectionViewLayoutAttributes.
52
+ layoutAttributesForCellWithIndexPath(path).tap do |obj|
53
+
54
+ x = (path.row + 0.5) * collectionView.frame.size.width
55
+ y = collectionView.frame.size.height / 2
56
+
57
+ obj.center = [x, y]
58
+ obj.size = [collectionView.frame.size.width, collectionView.frame.size.height]
59
+ end
60
+ end
61
+
62
+ def targetContentOffsetForProposedContentOffset proposedOffset,
63
+ withScrollingVelocity: velocity
64
+ offset = proposedOffset
65
+ index = selectElementAtPosition proposedOffset.x
66
+
67
+ offset.x = centerOfElementAtIndex index
68
+
69
+ offset
70
+
71
+ proposedOffset
72
+ end
73
+
74
+ def centerOfElementAtIndex index
75
+ unless @centerPostions.has_key? index
76
+ pos = index * collectionView.frame.size.width
77
+ @centerPostions[index] = pos
78
+ end
79
+
80
+ @centerPostions[index]
81
+ end
82
+
83
+ private
84
+
85
+ def centerOfPage? pos
86
+ @centerPostions.has_value? pos.round
87
+ end
88
+
89
+ def selectElementAtPosition pos
90
+ index = indexOfElementAtPosition pos
91
+
92
+ if index != delegate.selectedIndex
93
+ delegate.selectedIndex = index
94
+ end
95
+
96
+ index
97
+ end
98
+
99
+ def indexOfElementAtPosition position
100
+ (position / collectionView.frame.size.width).round
101
+ end
102
+
103
+ end
@@ -0,0 +1,22 @@
1
+ class TitlePage < UICollectionViewCell
2
+
3
+ attr_accessor :titleLabel
4
+
5
+ def initWithFrame frame
6
+ super
7
+
8
+ @titleLabel = UILabel.withZeroFrame.tap do |l|
9
+ l.backgroundColor = UIColor.clearColor
10
+ l.textAlignment = NSTextAlignmentCenter
11
+ end
12
+ contentView.addSubview @titleLabel
13
+
14
+ self
15
+ end
16
+
17
+ def layoutSubviews
18
+ super
19
+ @titleLabel.frame = [[0, 0], [frame.size.width, frame.size.height]]
20
+ end
21
+
22
+ end
@@ -0,0 +1,3 @@
1
+ module SwipeTitle
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "swipe_title/version"
2
+
3
+ if defined?(Motion::Project::Config)
4
+ Motion::Project::App.setup do |app|
5
+ app.files.unshift File.join(File.dirname(__FILE__), 'swipe_title/swipe_collection_view_controller.rb')
6
+ app.files.unshift File.join(File.dirname(__FILE__), 'swipe_title/swipe_title_controller.rb')
7
+ app.files.unshift File.join(File.dirname(__FILE__), 'swipe_title/swipe_title_layout.rb')
8
+ app.files.unshift File.join(File.dirname(__FILE__), 'swipe_title/title_page.rb')
9
+ end
10
+ end
11
+
12
+ module SwipeTitle
13
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'swipe_title'
@@ -0,0 +1,9 @@
1
+ describe SwipeTitle do
2
+ it 'should have a version number' do
3
+ SwipeTitle::VERSION.should_not be_nil
4
+ end
5
+
6
+ it 'should do something useful' do
7
+ false.should be_true
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swipe_title/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "swipe_title"
8
+ gem.version = SwipeTitle::VERSION
9
+ gem.authors = ["DmitryTsepelev"]
10
+ gem.email = ["dmitry.a.tsepelev@gmail.com"]
11
+ gem.description = "Swipe title view for RubyMotion projects"
12
+ gem.summary = "View which contains array of scrollable titles and page control"
13
+ gem.homepage = ""
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "rake"
22
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swipe_title
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - DmitryTsepelev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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
+ description: Swipe title view for RubyMotion projects
31
+ email:
32
+ - dmitry.a.tsepelev@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/swipe_title.rb
44
+ - lib/swipe_title/.DS_Store
45
+ - lib/swipe_title/swipe_collection_view_controller.rb
46
+ - lib/swipe_title/swipe_title_controller.rb
47
+ - lib/swipe_title/swipe_title_layout.rb
48
+ - lib/swipe_title/title_page.rb
49
+ - lib/swipe_title/version.rb
50
+ - spec/spec_helper.rb
51
+ - spec/swipe_title_spec.rb
52
+ - swipe_title.gemspec
53
+ homepage: ''
54
+ licenses:
55
+ - MIT
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: -4201674718821223087
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: -4201674718821223087
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.24
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: View which contains array of scrollable titles and page control
84
+ test_files:
85
+ - spec/spec_helper.rb
86
+ - spec/swipe_title_spec.rb