Class: SugarCube::AnimationChain

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnimationChain

Returns a new instance of AnimationChain.



20
21
22
23
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 20

def initialize
  raise "animation chains cannot be nested" if Thread.current[:sugarcube_chaining]
  @blocks = []
end

Class Method Details

.chainsObject



6
7
8
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 6

def chains
  @chains ||= []
end

.start_chain(chain) ⇒ Object



10
11
12
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 10

def start_chain(chain)
  chains << chain unless chains.include?(chain)
end

.stop_chain(chain) ⇒ Object



14
15
16
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 14

def stop_chain(chain)
  chains.delete(chain)
end

Instance Method Details

#<<(block) ⇒ Object



39
40
41
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 39

def << block
  and_then(&block)
end

#abortObject

stops the animation immediately



94
95
96
97
98
99
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 94

def abort
  return unless @running
  @loop = nil
  @abort = true
  @running = false
end

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



29
30
31
32
33
34
35
36
37
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 29

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

#do_nextObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 43

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

  options, block = @blocks[@block_index]
  @after_block = ->(completed) do
    if @abort || ! self.do_next
      @running = false
      if @loop
        start
      else
        AnimationChain.stop_chain(self)
      end
    end
  end.weak!
  options[:after] = @after_block

  UIView.animate(options) do
    Thread.current[:sugarcube_chaining] = true
    block.call if block
    Thread.current[:sugarcube_chaining] = nil
    @block_index += 1
  end
  true
end

#loop(times = true) ⇒ Object

Parameters:

  • times (Fixnum, nil) (defaults to: true)

    number of times to loop, or any other truthy value to loop forever



83
84
85
86
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 83

def loop(times=true)
  @loop = times
  start
end

#startObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 68

def start
  return if @running
  AnimationChain.start_chain(self)
  @running = true
  @abort = nil
  @block_index = 0
  if Fixnum === @loop
    @loop -= 1
    @loop = nil if @loop == 0
  end
  do_next
  return self
end

#stopObject

Cancels a loop, but lets the chain finish



89
90
91
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 89

def stop
  @loop = nil
end

#wait(duration) ⇒ Object



25
26
27
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 25

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