Class: Walt::Animation

Inherits:
Object
  • Object
show all
Extended by:
Support::AttrDefault
Defined in:
lib/walt/animation.rb

Constant Summary collapse

PROPERTIES =
[:delay, :duration, :after, :operations, :options]

Instance Method Summary collapse

Methods included from Support::AttrDefault

attr_default

Constructor Details

#initialize(params = {}) ⇒ Animation

Returns a new instance of Animation.



29
30
31
32
33
34
35
# File 'lib/walt/animation.rb', line 29

def initialize(params = {})
  params.each do |key, value|
    if PROPERTIES.include?(key.to_sym)
      self.send("#{key}=", value)
    end
  end
end

Instance Method Details

#after=(after) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/walt/animation.rb', line 37

def after=(after)
  _after = after
  case after
  when Walt::Animation
  when NSDictionary
    _after = Walt::Animation.new(after)
  else
    raise "Invalid class for after animation #{after.inspect}"
  end

  @after = _after
end

#animateObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/walt/animation.rb', line 99

def animate
  @operations_completed = 0
  self.operations.each do |operation|
    asset = self.assets[operation.id]
    if !asset
      raise "No asset found for operation #{operation} id #{operation.id}"
    end

    operation.setup(asset.view, self)

    UIView.animateWithDuration(self.duration,
      delay:self.delay.to_f,
      options:self.animation_options,
      animations:lambda {
        operation.finalize(asset.view, self)
      },
      completion:lambda { |completed|
        @operations_completed += 1
        if @operations_completed == self.operations.count && self.after
          self.after.assets = self.assets
          self.after.animate
        end
      }
    )
  end
end

#animation_optionsObject



93
94
95
96
97
# File 'lib/walt/animation.rb', line 93

def animation_options
  int = 0
  self.options.each { |i| int = int | Walt::Support.constant("UIViewAnimationOption", i)}
  int
end

#assets=(assets) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/walt/animation.rb', line 67

def assets=(assets)
  _assets = {}
  case assets
  when NSArray
    assets.each do |asset|
      _asset = (asset.is_a?(Walt::Asset) ? asset : Walt::Asset.for(asset))
      _assets[_asset.id] = _asset
    end
  when NSDictionary
    assets.each do |key, value|
      asset = value
      _asset = (asset.is_a?(Walt::Asset) ? asset : Walt::Asset.for(asset))
      _assets[_asset.id] = _asset
    end
  else
    raise "Not a valid class for assets: #{assets.inspect}"
  end
  @assets = _assets
end

#operations=(operations) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/walt/animation.rb', line 51

def operations=(operations)
  _operations = []
  operations.each do |op|
    case op
    when Walt::Operation::Base
      _operations << op
    when NSDictionary
      _operations << Walt::Operation.for(op)
    else
      raise "Invalid class for operation #{op.inspect}"
    end
  end
  @operations = _operations
end

#options=(options) ⇒ Object



88
89
90
91
# File 'lib/walt/animation.rb', line 88

def options=(options)
  _options = options.is_a?(NSArray) ? options : [options]
  @options = _options
end