Class: UnderOs::UI::Animation::Animation

Inherits:
Object
  • Object
show all
Defined in:
lib/under_os/ui/utils/animation.rb

Constant Summary collapse

CURVES =
{
  ease_in_out: UIViewAnimationOptionCurveEaseInOut,
      ease_in: UIViewAnimationOptionCurveEaseIn,
     ease_out: UIViewAnimationOptionCurveEaseOut,
       linear: UIViewAnimationOptionCurveLinear
}.freeze
FX_QUEUE =
{}

Instance Method Summary collapse

Constructor Details

#initialize(view, options, &block) ⇒ Animation

Returns a new instance of Animation.



45
46
47
48
49
50
51
52
53
# File 'lib/under_os/ui/utils/animation.rb', line 45

def initialize(view, options, &block)
  @view    = view
  @options = options
  @block   = block

  @options = {duration: options} if options.is_a?(Numeric)

  @options[:schedule] == false ? run : schedule
end

Instance Method Details

#delayObject



81
82
83
# File 'lib/under_os/ui/utils/animation.rb', line 81

def delay
  @options[:delay] || 0.0
end

#durationObject



77
78
79
# File 'lib/under_os/ui/utils/animation.rb', line 77

def duration
  @options[:duration] || 0.25
end

#optionsObject



85
86
87
88
89
90
# File 'lib/under_os/ui/utils/animation.rb', line 85

def options
  options = CURVES[@options[:curve]] || @options[:curve] || UIViewAnimationCurveEaseOut
  options = options | UIViewAnimationOptionAutoreverse if @options[:autoreverse]
  options = options | UIViewAnimationOptionRepeat      if @options[:repeat]
  options | UIViewAnimationOptionAllowUserInteraction
end

#queueObject



55
56
57
# File 'lib/under_os/ui/utils/animation.rb', line 55

def queue
  FX_QUEUE[@view] ||= []
end

#runObject



67
68
69
70
71
72
73
74
75
# File 'lib/under_os/ui/utils/animation.rb', line 67

def run
  @view.emit('animation:start')

  UIView.animateWithDuration duration,
                      delay: delay,
                    options: options,
                 animations: ->{ @block.call },
                 completion: ->(finished){ run_next }
end

#run_nextObject



92
93
94
95
96
97
98
99
# File 'lib/under_os/ui/utils/animation.rb', line 92

def run_next
  @view.emit('animation:finished')
  @options[:complete].call if @options[:complete]

  if next_fx = queue.shift
    next_fx.run
  end
end

#scheduleObject



59
60
61
62
63
64
65
# File 'lib/under_os/ui/utils/animation.rb', line 59

def schedule
  if queue.empty?
    run
  else
    queue << self
  end
end