Class: Formotion::RowType::SliderRow

Inherits:
Base show all
Includes:
BW::KVO
Defined in:
lib/formotion/row_type/slider_row.rb

Constant Summary collapse

SLIDER_VIEW_TAG =
1200

Instance Attribute Summary

Attributes inherited from Base

#row, #tableView

Instance Method Summary collapse

Methods inherited from Base

#_on_select, #after_build, #after_delete, #break_with_semaphore, #button?, #cellEditingStyle, #cell_style, #delete_row, field_buffer, #indentWhileEditing?, #initialize, #on_delete, #on_select, #update_cell, #with_semaphore

Constructor Details

This class inherits a constructor from Formotion::RowType::Base

Instance Method Details

#build_cell(cell) ⇒ Object



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
46
47
48
49
50
51
# File 'lib/formotion/row_type/slider_row.rb', line 10

def build_cell(cell)
  cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleNone
  slideView = UISlider.alloc.initWithFrame(CGRectZero)
  cell.accessoryView = cell.editingAccessoryView = slideView
  row.range ||= (1..10)
  slideView.minimumValue = row.range.first
  slideView.maximumValue = row.range.last
  slideView.tag = SLIDER_VIEW_TAG
  slideView.setValue(row.value, animated:true) if row.value
  slideView.accessibilityLabel = (row.title || "") + " Slider"
  slideView.userInteractionEnabled = row.editable?

  slideView.when(UIControlEventValueChanged) do
    break_with_semaphore do
      row.value = slideView.value
    end
  end
  observe(self.row, "value") do |old_value, new_value|
    break_with_semaphore do
      slideView.setValue(row.value, animated:false)
    end
  end


  cell.swizzle(:layoutSubviews) do
    def layoutSubviews
      old_layoutSubviews

      # viewWithTag is terrible, but I think it's ok to use here...
      formotion_field = self.viewWithTag(SLIDER_VIEW_TAG)
      formotion_field.sizeToFit

      field_frame = formotion_field.frame
      field_frame.origin.y = 10
      field_frame.origin.x = self.textLabel.frame.origin.x + self.textLabel.frame.size.width + Formotion::RowType::Base.field_buffer
      field_frame.size.width  = self.frame.size.width - field_frame.origin.x - Formotion::RowType::Base.field_buffer
      field_frame.size.height = self.frame.size.height - Formotion::RowType::Base.field_buffer
      formotion_field.frame = field_frame
    end
  end
  nil
end