class SugarCube::AnimationChain

Public Class Methods

chains() click to toggle source
# File lib/ios/sugarcube-animations/animation_chain.rb, line 6
def chains
  @chains ||= []
end
new() click to toggle source
# 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
start_chain(chain) click to toggle source
# File lib/ios/sugarcube-animations/animation_chain.rb, line 10
def start_chain(chain)
  chains << chain unless chains.include?(chain)
end
stop_chain(chain) click to toggle source
# File lib/ios/sugarcube-animations/animation_chain.rb, line 14
def stop_chain(chain)
  chains.delete(chain)
end

Public Instance Methods

<<(block) click to toggle source
# File lib/ios/sugarcube-animations/animation_chain.rb, line 39
def << block
  and_then(&block)
end
abort() click to toggle source

stops the animation immediately

# 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) click to toggle source
# 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_next() click to toggle source
# 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) click to toggle source

@param times [Fixnum,nil] number of times to loop, or any other truthy value to loop forever

# File lib/ios/sugarcube-animations/animation_chain.rb, line 83
def loop(times=true)
  @loop = times
  start
end
start() click to toggle source
# 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
stop() click to toggle source

Cancels a loop, but lets the chain finish

# File lib/ios/sugarcube-animations/animation_chain.rb, line 89
def stop
  @loop = nil
end
wait(duration) click to toggle source
# File lib/ios/sugarcube-animations/animation_chain.rb, line 25
def wait(duration)
  and_then(duration: duration) {}
end