motion-keyboard-avoiding 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 097671b1dfbd621e144301bc1b578c939c6b160d
4
+ data.tar.gz: 38183e3c52e70f967d6fdf8033990cc123af1d90
5
+ SHA512:
6
+ metadata.gz: 0ecaa60e4378a91d89207af2bf1220099ee229884af7ff560bf4224d711a66fb32521cbcd789f8b5f0dbad2a9b5093b48980e0e596b1e4ed6ab3fd18a7e5d7c2
7
+ data.tar.gz: 594ec5b7b1d3d68c43565eee0b0a97a19cb01268d979934c3ad0c83951dc232ef43feaaf3cf2ff152d59ba5f35cb66c68748f500c79bb28d8d85e5f57b297456
@@ -0,0 +1,39 @@
1
+ # motion-keyboard-avoiding
2
+
3
+ ## Usage
4
+
5
+ ```ruby
6
+ def viewDidLoad
7
+ super
8
+
9
+ @keyboard_avoiding = Motion::KeyboardAvoiding.new(view)
10
+
11
+ ...
12
+ # configure text field(s)
13
+ ...
14
+
15
+ @text_field.delegate = @keyboard_avoiding
16
+ end
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'motion-keyboard-avoiding'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install motion-keyboard-avoiding
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
6
+ Motion::Project::App.setup do |app|
7
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
8
+ end
@@ -0,0 +1,97 @@
1
+ class Motion
2
+ class KeyboardAvoiding
3
+ KEYBOARD_ANIMATION_DURATION = 0.3
4
+ MINIMUM_SCROLL_FRACTION = 0.2
5
+ MAXIMUM_SCROLL_FRACTION = 0.8
6
+ PORTRAIT_KEYBOARD_HEIGHT = 216.0
7
+ LANDSCAPE_KEYBOARD_HEIGHT = 162.0
8
+
9
+ attr_accessor :view, :animated_distance, :active_text_field
10
+
11
+ def initialize(view)
12
+ self.view = view
13
+
14
+ listen
15
+
16
+ view.instance_eval do
17
+ def willMoveToWindow(window)
18
+ NSNotificationCenter.defaultCenter.postNotificationName('WillMoveToWindow', object: self)
19
+ end
20
+ end
21
+
22
+ add_tap_recognizer
23
+ end
24
+
25
+ def listen
26
+ NSNotificationCenter.defaultCenter.addObserver(self, selector: 'reset:', name: 'WillMoveToWindow', object: view)
27
+ end
28
+
29
+ def reset(notification)
30
+ active_text_field.resignFirstResponder if active_text_field
31
+ end
32
+
33
+ def add_tap_recognizer
34
+ view.addGestureRecognizer(tap_recognizer)
35
+ end
36
+
37
+ def tap_recognizer
38
+ UITapGestureRecognizer.alloc.initWithTarget(self, action: 'dismiss_keyboard')
39
+ end
40
+
41
+ def dismiss_keyboard
42
+ active_text_field.resignFirstResponder if active_text_field
43
+ end
44
+
45
+ def textFieldDidBeginEditing(text_field)
46
+ self.active_text_field = text_field
47
+
48
+ text_field_rect = view.window.convertRect(text_field.bounds, fromView: text_field)
49
+ view_rect = view.window.convertRect(view.bounds, fromView: view)
50
+ midline = text_field_rect.origin.y + 0.5 * text_field_rect.size.height
51
+ numerator = midline - view_rect.origin.y - MINIMUM_SCROLL_FRACTION * view_rect.size.height
52
+ denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * view_rect.size.height
53
+ height_fraction = numerator / denominator
54
+
55
+ if height_fraction < 0.0
56
+ height_fraction = 0.0
57
+ elsif height_fraction > 1.0
58
+ height_fraction = 1.0
59
+ end
60
+
61
+ orientation = UIApplication.sharedApplication.statusBarOrientation
62
+ if orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown
63
+ self.animated_distance = (PORTRAIT_KEYBOARD_HEIGHT * height_fraction).floor
64
+ else
65
+ self.animated_distance = (LANDSCAPE_KEYBOARD_HEIGHT * height_fraction).floor
66
+ end
67
+
68
+ view_frame = view.frame
69
+ view_frame.origin.y -= animated_distance
70
+
71
+ set_frame(view_frame)
72
+ end
73
+
74
+ def textFieldDidEndEditing(text_field)
75
+ view_frame = view.frame
76
+ view_frame.origin.y += animated_distance
77
+
78
+ set_frame(view_frame)
79
+ end
80
+
81
+ def set_frame(frame)
82
+ UIView.beginAnimations(nil, context: nil)
83
+ UIView.setAnimationBeginsFromCurrentState(true)
84
+ UIView.setAnimationDuration(KEYBOARD_ANIMATION_DURATION)
85
+
86
+ view.setFrame(frame)
87
+
88
+ UIView.commitAnimations
89
+ end
90
+
91
+ def textFieldShouldReturn(text_field)
92
+ text_field.resignFirstResponder
93
+
94
+ true
95
+ end
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-keyboard-avoiding
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Devon Blandin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Make iOS text fields avoid the keyboard
28
+ email:
29
+ - dblandin@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - lib/motion-keyboard-avoiding.rb
36
+ - lib/project/motion-keyboard-avoiding.rb
37
+ homepage: https://github.com/dblandin/motion-keyboard-avoiding
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.0.0
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Make iOS text fields avoid the keyboard
61
+ test_files: []