Sha256: b5856b25987260f3089af15b083c4be920aef9000c8be46c13bc34354acf20ed

Contents?: true

Size: 1.96 KB

Versions: 27

Compression:

Stored size: 1.96 KB

Contents

module SugarCube
  class AnimationChain

    class << self

      def chains
        @chains ||= []
      end

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

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

    end

    def initialize
      raise "animation chains cannot be nested" if Thread.current[:sugarcube_chaining]
      @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) 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

    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

    # @param times [Fixnum,nil] number of times to loop, or any other truthy value to loop forever
    def loop(times=true)
      @loop = times
      start
    end

    # Cancels a loop, but lets the chain finish
    def stop
      @loop = nil
    end

    # stops the animation immediately
    def abort
      return unless @running
      @loop = nil
      @abort = true
      @running = false
    end

  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
sugarcube-4.0.0 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.5.0 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.4.2 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.4.1 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.4.0 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.7 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.6 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.5 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.4 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.3 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.2 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.1 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.3.0 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.2.0 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.1.1 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.1.0 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.0.8 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.0.7 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.0.6 lib/ios/sugarcube-animations/animation_chain.rb
sugarcube-3.0.5 lib/ios/sugarcube-animations/animation_chain.rb