Class: Motion::KeyboardAvoiding
- Inherits:
-
Object
- Object
- Motion::KeyboardAvoiding
- Defined in:
- lib/project/motion-keyboard-avoiding.rb
Constant Summary collapse
- KEYBOARD_ANIMATION_DURATION =
0.3
- MINIMUM_SCROLL_FRACTION =
0.2
- MAXIMUM_SCROLL_FRACTION =
0.8
- PORTRAIT_KEYBOARD_HEIGHT =
216.0
- LANDSCAPE_KEYBOARD_HEIGHT =
162.0
Instance Attribute Summary collapse
-
#active_text_field ⇒ Object
Returns the value of attribute active_text_field.
-
#animated_distance ⇒ Object
Returns the value of attribute animated_distance.
-
#view ⇒ Object
Returns the value of attribute view.
Instance Method Summary collapse
- #add_tap_recognizer ⇒ Object
- #dismiss_keyboard ⇒ Object
-
#initialize(view) ⇒ KeyboardAvoiding
constructor
A new instance of KeyboardAvoiding.
- #listen ⇒ Object
- #reset(notification) ⇒ Object
- #set_frame(frame) ⇒ Object
- #tap_recognizer ⇒ Object
- #textFieldDidBeginEditing(text_field) ⇒ Object
- #textFieldDidEndEditing(text_field) ⇒ Object
- #textFieldShouldReturn(text_field) ⇒ Object
Constructor Details
#initialize(view) ⇒ KeyboardAvoiding
Returns a new instance of KeyboardAvoiding.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 11 def initialize(view) self.view = view listen view.instance_eval do def willMoveToWindow(window) NSNotificationCenter.defaultCenter.postNotificationName('WillMoveToWindow', object: self) end end add_tap_recognizer end |
Instance Attribute Details
#active_text_field ⇒ Object
Returns the value of attribute active_text_field.
9 10 11 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 9 def active_text_field @active_text_field end |
#animated_distance ⇒ Object
Returns the value of attribute animated_distance.
9 10 11 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 9 def animated_distance @animated_distance end |
#view ⇒ Object
Returns the value of attribute view.
9 10 11 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 9 def view @view end |
Instance Method Details
#add_tap_recognizer ⇒ Object
33 34 35 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 33 def add_tap_recognizer view.addGestureRecognizer(tap_recognizer) end |
#dismiss_keyboard ⇒ Object
41 42 43 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 41 def dismiss_keyboard active_text_field.resignFirstResponder if active_text_field end |
#listen ⇒ Object
25 26 27 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 25 def listen NSNotificationCenter.defaultCenter.addObserver(self, selector: 'reset:', name: 'WillMoveToWindow', object: view) end |
#reset(notification) ⇒ Object
29 30 31 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 29 def reset(notification) active_text_field.resignFirstResponder if active_text_field end |
#set_frame(frame) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 81 def set_frame(frame) UIView.beginAnimations(nil, context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(KEYBOARD_ANIMATION_DURATION) view.setFrame(frame) UIView.commitAnimations end |
#tap_recognizer ⇒ Object
37 38 39 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 37 def tap_recognizer UITapGestureRecognizer.alloc.initWithTarget(self, action: 'dismiss_keyboard') end |
#textFieldDidBeginEditing(text_field) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 45 def textFieldDidBeginEditing(text_field) self.active_text_field = text_field text_field_rect = view.window.convertRect(text_field.bounds, fromView: text_field) view_rect = view.window.convertRect(view.bounds, fromView: view) midline = text_field_rect.origin.y + 0.5 * text_field_rect.size.height numerator = midline - view_rect.origin.y - MINIMUM_SCROLL_FRACTION * view_rect.size.height denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * view_rect.size.height height_fraction = numerator / denominator if height_fraction < 0.0 height_fraction = 0.0 elsif height_fraction > 1.0 height_fraction = 1.0 end orientation = UIApplication.sharedApplication.statusBarOrientation if orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown self.animated_distance = (PORTRAIT_KEYBOARD_HEIGHT * height_fraction).floor else self.animated_distance = (LANDSCAPE_KEYBOARD_HEIGHT * height_fraction).floor end view_frame = view.frame view_frame.origin.y -= animated_distance set_frame(view_frame) end |
#textFieldDidEndEditing(text_field) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 74 def textFieldDidEndEditing(text_field) view_frame = view.frame view_frame.origin.y += animated_distance || 0.0 set_frame(view_frame) end |
#textFieldShouldReturn(text_field) ⇒ Object
91 92 93 94 95 |
# File 'lib/project/motion-keyboard-avoiding.rb', line 91 def textFieldShouldReturn(text_field) text_field.resignFirstResponder true end |