Class: SugarCube::AnimationChain
- Defined in:
- lib/ios/sugarcube-animations/animation_chain.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<<(block) ⇒ Object
-
#abort ⇒ Object
stops the animation immediately.
- #and_then(options = nil, &block) ⇒ Object
- #do_next ⇒ Object
-
#initialize ⇒ AnimationChain
constructor
A new instance of AnimationChain.
- #loop(times = true) ⇒ Object
- #start ⇒ Object
-
#stop ⇒ Object
Cancels a loop, but lets the chain finish.
- #wait(duration) ⇒ Object
Constructor Details
#initialize ⇒ AnimationChain
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
.chains ⇒ Object
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 |
#abort ⇒ Object
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(=nil, &block) if = .dup else = {} end @blocks << [, block] self end |
#do_next ⇒ Object
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 , 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! [:after] = @after_block UIView.animate() 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
83 84 85 86 |
# File 'lib/ios/sugarcube-animations/animation_chain.rb', line 83 def loop(times=true) @loop = times start end |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |