Class: Tgios::UICollectionViewBinding

Inherits:
BindingBase show all
Includes:
PlasticCup
Defined in:
lib/tgios/ui_collection_view_binding.rb

Direct Known Subclasses

ImagesCollectionViewBinding

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BindingBase

#dealloc, #hook, #prepareForRelease, #unhook

Constructor Details

#initialize(*args) ⇒ UICollectionViewBinding

Returns a new instance of UICollectionViewBinding.



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

def initialize(*args)
  super
  @events={}
  @events[:setup_cell]=->(cell, subview, index_path) { setup_cell(cell, subview, index_path) }.weak!
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



5
6
7
# File 'lib/tgios/ui_collection_view_binding.rb', line 5

def current_page
  @current_page
end

Class Method Details

.new_horizontal_collection_view(options = {}) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/tgios/ui_collection_view_binding.rb', line 86

def self.new_horizontal_collection_view(options={})
  layout = Base.style(UICollectionViewFlowLayout.new,
                      {scrollDirection: UICollectionViewScrollDirectionHorizontal,
                       sectionInset:UIEdgeInsetsMake(0, 15, 0, 10)}.merge(options[:layout] || {}))
  Base.style(UICollectionView.alloc.initWithFrame(CGRectZero, collectionViewLayout: layout),
             {backgroundColor: :white.uicolor}.merge(options[:view] || {})
  )
end

Instance Method Details

#bind(collection_view, views = nil) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/tgios/ui_collection_view_binding.rb', line 18

def bind(collection_view, views=nil)
  @collection_view = WeakRef.new(collection_view)
  @collection_view.registerClass(UICollectionViewCell, forCellWithReuseIdentifier:'UICollectionViewCell')
  @views = WeakRef.new(views) unless views.nil?
  @collection_view.delegate = self
  @collection_view.dataSource = self
  self
end

#collectionView(collectionView, layout: collectionViewLayout, sizeForItemAtIndexPath: indexPath) ⇒ Object



32
33
34
# File 'lib/tgios/ui_collection_view_binding.rb', line 32

def collectionView(collectionView, numberOfItemsInSection: section)
  @views.count
end

#go_to_page(page, animated = false) ⇒ Object



70
71
72
73
74
75
# File 'lib/tgios/ui_collection_view_binding.rb', line 70

def go_to_page(page, animated=false)
  frame = @collection_view.bounds
  frame.origin.x = frame.size.width * page
  @collection_view.scrollRectToVisible(frame, animated: animated)
  @events[:page_changed].call(page) unless @events[:page_changed].nil?
end

#on(event_name, &block) ⇒ Object



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

def on(event_name, &block)
  @events[event_name]=block.weak!
  self
end

#onPrepareForReleaseObject



95
96
97
98
99
100
101
# File 'lib/tgios/ui_collection_view_binding.rb', line 95

def onPrepareForRelease
  @events=nil
  @collection_view.delegate = nil
  @collection_view.dataSource = nil
  @collection_view = nil
  @views = nil
end

#reload(views = nil) ⇒ Object



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

def reload(views=nil)
  @views = WeakRef.new(views) unless views.nil?
  @collection_view.reloadData if @collection_view.weakref_alive?
end

#scrollViewDidEndDecelerating(scrollView) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/tgios/ui_collection_view_binding.rb', line 58

def scrollViewDidEndDecelerating(scrollView)
  page = (scrollView.contentOffset.x / scrollView.frame.size.width).round
  if page != @current_page
    @current_page = page
    @events[:page_changed].call(page) unless @events[:page_changed].nil?
  end
end

#scrollViewDidScroll(scroll_view) ⇒ Object



66
67
68
# File 'lib/tgios/ui_collection_view_binding.rb', line 66

def scrollViewDidScroll(scroll_view)
  @events[:view_did_scroll].call(scroll_view) if @events[:view_did_scroll]
end

#setup_cell(cell, subview, index_path) ⇒ Object



81
82
83
84
# File 'lib/tgios/ui_collection_view_binding.rb', line 81

def setup_cell(cell, subview, index_path)
  cell.contentView.subviews.makeObjectsPerformSelector('removeFromSuperview')
  cell.contentView.addSubview(subview)
end

#view_at(index_path) ⇒ Object



77
78
79
# File 'lib/tgios/ui_collection_view_binding.rb', line 77

def view_at(index_path)
  @views[index_path.row]
end