Module: Loco::Resizable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Observable

#init, #initialize, #method_missing, #register_observer, #remove_all_observers, #remove_observer, #set_properties, #update_attributes

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Loco::Observable

Instance Attribute Details

#bottomInteger

The position of the bottom edge, relative to the superview’s bottom edge.

Returns:

  • (Integer)


16
17
18
# File 'lib/motion-loco/resizable.rb', line 16

def bottom
  @bottom
end

#heightInteger

The height of the view.

Returns:

  • (Integer)


35
36
37
# File 'lib/motion-loco/resizable.rb', line 35

def height
  @height
end

#leftInteger

The position of the left edge, relative to the superview’s left edge.

Returns:

  • (Integer)


51
52
53
# File 'lib/motion-loco/resizable.rb', line 51

def left
  @left
end

#parent_viewObject

Returns the value of attribute parent_view.



5
6
7
# File 'lib/motion-loco/resizable.rb', line 5

def parent_view
  @parent_view
end

#parentViewObject

Returns the value of attribute parentView.



5
6
7
# File 'lib/motion-loco/resizable.rb', line 5

def parentView
  @parentView
end

#rightInteger

The position of the right edge, relative to the superview’s right edge.

Returns:

  • (Integer)


67
68
69
# File 'lib/motion-loco/resizable.rb', line 67

def right
  @right
end

#topInteger

The position of the top edge, relative to the superview’s top edge.

Returns:

  • (Integer)


83
84
85
# File 'lib/motion-loco/resizable.rb', line 83

def top
  @top
end

#widthInteger

The width of the view.

Returns:

  • (Integer)


98
99
100
# File 'lib/motion-loco/resizable.rb', line 98

def width
  @width
end

Class Method Details

.included(base) ⇒ Object



305
306
307
# File 'lib/motion-loco/resizable.rb', line 305

def self.included(base)
  base.extend(Observable::ClassMethods)
end

Instance Method Details

#initWithFrame(properties = {}) ⇒ Object

Create new instance from a hash of properties with values.

Parameters:

  • frame (Object)

    The CGRect or a Hash of properties.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/motion-loco/resizable.rb', line 113

def initWithFrame(properties={})
  if properties.is_a? Hash
    # Set the initial property values from the given hash
    super(CGRect.new)
    initialize_bindings
    set_properties(properties)
  else
    super(properties)
  end
  view_setup
  
  self
end

#refresh_layout(superview = nil) ⇒ Object Also known as: refreshLayout

Refresh the layout based on bottom, left, right, top, and superview.

Parameters:

  • superview (UIView) (defaults to: nil)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/motion-loco/resizable.rb', line 129

def refresh_layout(superview=nil)
  # The view can't be positioned without being added to the superview first.
  superview ||= self.superview
  return if superview.nil?

  # Determine the original size, position, and autoresizing mask that should be used.
  if self.top && self.bottom
    if self.left && self.right
      # FW, FH
      @origin_x     = self.left
      @origin_y     = self.top
      @size_height  = superview.bounds.size.height - self.top - self.bottom
      @size_width   = superview.bounds.size.width - self.left - self.right
      @autoresizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
    elsif self.left && self.width
      # FH, FRM
      @origin_x     = self.left
      @origin_y     = self.top
      @size_height  = superview.bounds.size.height - self.top - self.bottom
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin
    elsif self.right && self.width
      # FH, FLM
      @origin_x     = superview.bounds.size.width - self.width - self.right
      @origin_y     = self.top
      @size_height  = superview.bounds.size.height - self.top - self.bottom
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin
    elsif self.width
      # FH, FLM, FRM
      @origin_x     = (superview.bounds.size.width - self.width) / 2
      @origin_y     = self.top
      @size_height  = superview.bounds.size.height - self.top - self.bottom
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin
    else
      # Needs More Params
      NSLog('%@<Loco::UI::View> Not enough params to position and size the view.', self.class)
    end
  elsif self.top
    if self.left && self.right && self.height
      # FW, FBM
      @origin_x     = self.left
      @origin_y     = self.top
      @size_height  = self.height
      @size_width   = superview.bounds.size.width - self.left - self.right
      @autoresizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin
    elsif self.left && self.height && self.width
      # FBM, FRM
      @origin_x     = self.left
      @origin_y     = self.top
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin
    elsif self.right && self.height && self.width
      # FBM, FLM
      @origin_x     = superview.bounds.size.width - self.width - self.right
      @origin_y     = self.top
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin
    elsif self.height && self.width
      # FLM, FRM, FBM
      @origin_x     = (superview.bounds.size.width - self.width) / 2
      @origin_y     = self.top
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleBottomMargin
    else
      # Needs More Params
      NSLog('%@<Loco::UI::View> Not enough params to position and size the view.', self.class)
    end
  elsif self.bottom
    if self.left && self.right && self.height
      # FW, FTM
      @origin_x     = self.left
      @origin_y     = superview.bounds.size.height - self.height - self.bottom
      @size_height  = self.height
      @size_width   = superview.bounds.size.width - self.left - self.right
      @autoresizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin
    elsif self.left && self.height && self.width
      # FTM, FRM
      @origin_x     = self.left
      @origin_y     = superview.bounds.size.height - self.height - self.bottom
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin
    elsif self.right && self.height && self.width
      # FTM, FLM
      @origin_x     = superview.bounds.size.width - self.width - self.right
      @origin_y     = superview.bounds.size.height - self.height - self.bottom
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin
    elsif self.height && self.width
      # FLM, FRM, FTM
      @origin_x     = (superview.bounds.size.width - self.width) / 2
      @origin_y     = superview.bounds.size.height - self.height - self.bottom
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin
    else
      # Needs More Params
      NSLog('%@<Loco::UI::View> Not enough params to position and size the view.', self.class)
    end
  elsif self.left && self.right && self.height
    # FW, FTM, FBM
    @origin_x     = self.left
    @origin_y     = (superview.bounds.size.height - self.height) / 2
    @size_height  = self.height
    @size_width   = superview.bounds.size.width - self.left - self.right
    @autoresizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
  elsif self.width && self.height
    if self.left
      # FTM, FBM, FRM
      @origin_x     = self.left
      @origin_y     = (superview.bounds.size.height - self.height) / 2
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin
    elsif self.right
      # FTM, FBM, FLM
      @origin_x     = superview.bounds.size.width - self.width - self.right
      @origin_y     = (superview.bounds.size.height - self.height) / 2
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin
    else
      # FTM, FBM, FLM, FRM
      @origin_x     = (superview.bounds.size.width - self.width) / 2
      @origin_y     = (superview.bounds.size.height - self.height) / 2
      @size_height  = self.height
      @size_width   = self.width
      @autoresizing = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin
    end
  else
    # Needs More Params
    NSLog('%@<Loco::UI::View> Not enough params to position and size the view.', self.class)
  end
  
  # Warn of any possible conflicts
  if self.top && self.bottom && self.height
    NSLog('%@<Loco::UI::View> `top`, `bottom`, and `height` may conflict with each other. Only two of the three should be set.', self.class)
    NSLog('%@<Loco::UI::View> top: %@, bottom: %@, height: %@', self.class, self.top, self.bottom, self.height)
  end
  if self.left && self.right && self.width
    NSLog('%@<Loco::UI::View> `left`, `right`, and `width` may conflict with each other. Only two of the three should be set.', self.class)
    NSLog('%@<Loco::UI::View> left: %@, right: %@, width: %@', self.class, self.left, self.right, self.width)
  end
  
  if @origin_x && @origin_y && @size_width && @size_height && @autoresizing
    self.frame = [[@origin_x, @origin_y], [@size_width, @size_height]]
    self.autoresizingMask = @autoresizing
  end
  
  # Update the subviews
  self.subviews.each do |view|
    view.refresh_layout(self) if view.is_a? Resizable
  end
end

#view_setupObject



291
292
293
# File 'lib/motion-loco/resizable.rb', line 291

def view_setup
  viewSetup
end

#viewSetupObject



295
296
297
# File 'lib/motion-loco/resizable.rb', line 295

def viewSetup
  # Override #view_setup or #viewSetup to customize the view
end

#willMoveToSuperview(superview) ⇒ Object

Fires when the superview changes.



300
301
302
303
# File 'lib/motion-loco/resizable.rb', line 300

def willMoveToSuperview(superview)
  self.parent_view = superview
  refresh_layout(superview)
end