Class: SlidingCellView

Inherits:
MPTableCellWithSection
  • Object
show all
Defined in:
lib/prime_sliding_action/sliding_cell_view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pan_recognizerObject

Returns the value of attribute pan_recognizer.



2
3
4
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 2

def pan_recognizer
  @pan_recognizer
end

#sliding_action_buttonsObject

Returns the value of attribute sliding_action_buttons.



2
3
4
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 2

def sliding_action_buttons
  @sliding_action_buttons
end

#tap_recognizerObject

Returns the value of attribute tap_recognizer.



2
3
4
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 2

def tap_recognizer
  @tap_recognizer
end

Instance Method Details

#actions_visible?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 133

def actions_visible?
  @sliding_action_buttons_state == :visible
end

#cell_heightObject



137
138
139
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 137

def cell_height
  section ? section.container_options[:height] : self.height
end

#gestureRecognizerShouldBegin(gestureRecognizer) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 141

def gestureRecognizerShouldBegin(gestureRecognizer)
  if gestureRecognizer.is_a?(UIPanGestureRecognizer)
    velocity = (gestureRecognizer.velocityInView gestureRecognizer.view)
    velocity.y.abs < velocity.x.abs
  else
    true
  end
end

#hide_actionsObject



128
129
130
131
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 128

def hide_actions
  self.scroll_view.setContentOffset(CGPointMake(0, 0), animated: true)
  @sliding_action_buttons_state = :hidden
end

#initialize_contentObject



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
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 47

def initialize_content
  self.scroll_view = self.subviews.first
  # iOS 8
  if self.scroll_view.is_a?(UITableViewCellContentView)
    self.scroll_view.removeFromSuperview
    self.scroll_view = UIScrollView.alloc.initWithFrame(self.bounds)
    self.scroll_view.setBackgroundColor(:clear.uicolor)
    self.scroll_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
    self.scroll_view.top = 0
    self.scroll_view.left = 0
    self.addSubview(scroll_view)
  else
    self.scroll_view.subviews.first.removeFromSuperview
  end
  self.content_view = MPTableViewCellContentView.alloc.initWithFrame(CGRectMake(0,0,320, self.height))
  self.content_view.setBackgroundColor(:white.uicolor)
  self.content_view.autoresizingMask = UIViewAutoresizingFlexibleHeight

  self.scroll_view.addSubview(content_view)
  self.scroll_view.setDelegate self
  self.scroll_view.setScrollEnabled true
  self.scroll_view.setContentSize CGSizeMake(320, self.height)
  self.scroll_view.autoresizingMask = UIViewAutoresizingFlexibleHeight

  self.scroll_view.setShowsHorizontalScrollIndicator false
  self.scroll_view.setScrollsToTop false
  self.scroll_view.setPagingEnabled true

  self.pan_recognizer = UIPanGestureRecognizer.alloc.initWithTarget(self, action: 'pan')
  self.pan_recognizer.setMinimumNumberOfTouches 1
  self.pan_recognizer.setMaximumNumberOfTouches 1
  self.pan_recognizer.delegate = self
  self.addGestureRecognizer pan_recognizer

  self.tap_recognizer = UITapGestureRecognizer.alloc.initWithTarget(self, action: 'tap')
  self.tap_recognizer.delegate = self
  self.addGestureRecognizer tap_recognizer
end

#panObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 86

def pan
  offset = @sliding_action_buttons_offset || 0
  translation = pan_recognizer.translationInView self
  velocity = pan_recognizer.velocityInView self
  if pan_recognizer.state == UIGestureRecognizerStateEnded
    if velocity.x < -1000
      show_actions
    elsif velocity.x > 1000
      hide_actions
    elsif self.scroll_view.contentOffset.x > (offset / 2)
      show_actions
    else
      hide_actions
    end
  else
    if pan_recognizer.state == UIGestureRecognizerStateBegan
      @sliding_action_buttons_x = self.scroll_view.contentOffset.x
    end
    current_x = @sliding_action_buttons_x - translation.x
    if translation.x < 0 || current_x > 0
      self.scroll_view.setContentOffset CGPointMake(current_x, 0)
    end
  end
  0
end

#setSection(section) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 4

def setSection(section)
  @section = section.try(:weak_ref)
  self.content_view.setSection(@section)
  if section
    # init section
    @table = @section.collection_section.collection_view
    @table.directionalLockEnabled = true

    unless @sliding_action_buttons_rendered
      @sliding_action_buttons_offset = 0
      @sliding_action_buttons = {}
      @section.sliding_action_buttons.each do |action, options|
        styles = [:base_action_button, :"base_action_button_#{action}", :"#{@section.name}_action_button", :"#{@section.name}_action_button_#{action}"]
        button = @section.screen.add_view MPButton,
          top: 0,
          width: 80,
          left: @section.screen.view.width + @sliding_action_buttons_offset,
          height: cell_height,
          title_color: options[:title_color] || :white,
          title: options[:title],
          background_color: options[:background_color] || options[:color] || :red
        @section.screen.set_options_for button, styles: styles
        @sliding_action_buttons_offset += button.width
        button.on :touch do
          button.off(:touch) if options[:unbind]
          @section.send(options[:action])
          hide_actions unless (options.has_key?(:hide_on_finish) && !options[:hide_on_finish])
        end

        @sliding_action_buttons[action] = button

        self.scroll_view.addSubview button
      end
      self.scroll_view.setContentSize CGSizeMake(320 + @sliding_action_buttons_offset, cell_height)

      @sliding_action_buttons_rendered = true
      @sliding_action_buttons_state = :hidden
    end
  else
    @sliding_action_buttons_rendered = nil
  end
end

#show_actionsObject



122
123
124
125
126
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 122

def show_actions
  offset = @sliding_action_buttons_offset || 0
  self.scroll_view.setContentOffset(CGPointMake(offset, 0), animated: true)
  @sliding_action_buttons_state = :visible
end

#tapObject



112
113
114
115
116
117
118
119
120
# File 'lib/prime_sliding_action/sliding_cell_view.rb', line 112

def tap
  if @sliding_action_buttons_state == :hidden && @table
    cell_index = @table.indexPathForCell(self)
    @table.delegate.tableView(@table, didSelectRowAtIndexPath: cell_index)
  else
    hide_actions
  end
  0
end