Class: SugarCube::AnimationChain

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcube/animation_chain.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnimationChain

Returns a new instance of AnimationChain.



20
21
22
# File 'lib/sugarcube/animation_chain.rb', line 20

def initialize
  @blocks = []
end

Class Method Details

.chainsObject



5
6
7
# File 'lib/sugarcube/animation_chain.rb', line 5

def chains
  @chains ||= []
end

.start_chain(chain) ⇒ Object



9
10
11
# File 'lib/sugarcube/animation_chain.rb', line 9

def start_chain(chain)
  chains << chain
end

.stop_chain(chain) ⇒ Object



13
14
15
16
# File 'lib/sugarcube/animation_chain.rb', line 13

def stop_chain(chain)
  chains ||= []
  @chains.delete(chain)
end

Instance Method Details

#<<(block) ⇒ Object



38
39
40
# File 'lib/sugarcube/animation_chain.rb', line 38

def << block
  and_then(&block)
end

#and_then(options = nil, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/sugarcube/animation_chain.rb', line 28

def and_then(options=nil, &block)
  if options
    options = options.dup
  else
    options = {}
  end
  @blocks << [options, block]
  self
end

#do_nextObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sugarcube/animation_chain.rb', line 42

def do_next
  return nil if @block_index >= @blocks.length

  options, block = @blocks[@block_index]
  @after_block = ->(completed){
    self.do_next || AnimationChain.stop_chain(self)
  }
  options[:after] = @after_block

  UIView.animate(options) {
    block.call
    @block_index += 1
  }
  true
end

#startObject



58
59
60
61
62
# File 'lib/sugarcube/animation_chain.rb', line 58

def start
  AnimationChain.start_chain(self)
  @block_index = 0
  do_next
end

#wait(duration) ⇒ Object



24
25
26
# File 'lib/sugarcube/animation_chain.rb', line 24

def wait(duration)
  and_then(duration: duration) {}
end