Sha256: 424b634135a0ffc7d2d09d469670c8a3ab4ced3c60b3906272bd084b03047ae9

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

module SugarCube
  class AnimationChain
    class << self

      def chains
        @chains ||= []
      end

      def start_chain(chain)
        chains << chain
      end

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

    end

    def initialize
      @blocks = []
    end

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

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

    def << block
      and_then(&block)
    end

    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

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

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sugarcube-0.18.12 lib/sugarcube/animation_chain.rb
sugarcube-0.18.11 lib/sugarcube/animation_chain.rb
sugarcube-0.18.10 lib/sugarcube/animation_chain.rb
sugarcube-0.18.9 lib/sugarcube/animation_chain.rb
sugarcube-0.18.8 lib/sugarcube/animation_chain.rb
sugarcube-0.18.7 lib/sugarcube/animation_chain.rb