Class: Tgios::UITableViewUtilityBinding

Inherits:
BindingBase show all
Defined in:
lib/tgios/ui_table_view_utility_binding.rb

Overview

common usage: call listen_to_keyboard in viewWillAppear call stop_listen_to_keyboard in viewWillDisappear call will_scroll_to_index_path in text field begin_edit event

Instance Method Summary collapse

Methods inherited from BindingBase

#dealloc, #hook, #initialize, #prepareForRelease, #unhook

Constructor Details

This class inherits a constructor from Tgios::BindingBase

Instance Method Details

#bind(table) ⇒ Object



7
8
9
10
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 7

def bind(table)
  @table = WeakRef.new(table)
  self
end

#expand_table_view(note) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 31

def expand_table_view(note)
  @expanding = true
  offset_y = @table.contentOffset.y
  frame_height = @table.frame.size.height
  content_height = @table.contentSize.height
  new_offset_height = nil
  if frame_height > content_height
    if offset_y != 0
      new_offset_height = 0
    end
  else
    bottom_offset = frame_height - content_height + offset_y
    if bottom_offset > 0
      new_offset_height = offset_y - bottom_offset
    end
  end

  if new_offset_height.nil?
    reset_content_inset_bottom
  else
    curve = note[UIKeyboardAnimationCurveUserInfoKey]
    duration = note[UIKeyboardAnimationDurationUserInfoKey]
    @animation_proc = -> {
      @table.setContentOffset([0, new_offset_height])
    }
    @completion_proc = lambda { |finished|
      reset_content_inset_bottom
    }
    UIView.animateWithDuration(duration-0.01, delay: 0, options: curve,
                               animations: @animation_proc,
                               completion: @completion_proc)
  end
end

#keyboardWillHide(note) ⇒ Object



27
28
29
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 27

def keyboardWillHide(note)
  expand_table_view(note)
end

#keyboardWillShow(note) ⇒ Object



23
24
25
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 23

def keyboardWillShow(note)
  shrink_table_view(note)
end

#listen_to_keyboardObject



12
13
14
15
16
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 12

def listen_to_keyboard
  @scroll_when_editing = true
  NSNotificationCenter.defaultCenter.addObserver(self, selector: 'keyboardWillShow:', name: UIKeyboardWillShowNotification, object: nil)
  NSNotificationCenter.defaultCenter.addObserver(self, selector: 'keyboardWillHide:', name: UIKeyboardWillHideNotification, object: nil)
end

#onPrepareForReleaseObject



114
115
116
117
118
119
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 114

def onPrepareForRelease
  self.stop_listen_to_keyboard
  @animation_proc=nil
  @completion_proc=nil
  @table=nil
end

#reset_content_inset_bottomObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 78

def reset_content_inset_bottom
  return unless @table.weakref_alive?
  @table.contentInset = UIEdgeInsetsZero
  @table.scrollIndicatorInsets = UIEdgeInsetsZero
  @expanding = false
  if @shrink_height
    set_content_inset_bottom(@shrink_height)
    @shrink_height = nil
  end
end

#scroll_to_index_path(index_path) ⇒ Object



109
110
111
112
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 109

def scroll_to_index_path(index_path)
  @table.scrollToRowAtIndexPath(index_path, atScrollPosition: UITableViewScrollPositionMiddle, animated: true)
  @index_path_to_scroll = nil
end

#set_content_inset_bottom(height) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 89

def set_content_inset_bottom(height)
  @table.contentInset = UIEdgeInsetsMake(0,0,height,0)
  @table.scrollIndicatorInsets = UIEdgeInsetsMake(0,0,height,0)
  @shrinking = false
  if @index_path_to_scroll
    scroll_to_index_path(@index_path_to_scroll)
    @index_path_to_scroll = nil
  end
end

#shrink_table_view(note) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 65

def shrink_table_view(note)
  @shrinking = true
  rect = note[UIKeyboardFrameEndUserInfoKey].CGRectValue
  table_rect= @table.superview.convertRect(@table.frame, toView:nil)
  intersect = CGRectIntersection(rect, table_rect)
  expand_height = intersect.present? ? intersect.size.height : 0
  if @expanding
    @shrink_height = expand_height
  else
    set_content_inset_bottom(expand_height)
  end
end

#stop_listen_to_keyboardObject



18
19
20
21
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 18

def stop_listen_to_keyboard
  @scroll_when_editing = false
  NSNotificationCenter.defaultCenter.removeObserver(self)
end

#will_scroll_to_index_path(index_path) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/tgios/ui_table_view_utility_binding.rb', line 99

def will_scroll_to_index_path(index_path)
  if @scroll_when_editing
    if @shrinking
      @index_path_to_scroll = index_path
    else
      performSelector('scroll_to_index_path:', withObject: index_path, afterDelay:0.01)
    end
  end
end