Module: Loco::Resizable

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bottomInteger

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

Returns:

  • (Integer)


11
12
13
# File 'lib/motion-loco/resizable.rb', line 11

def bottom
  @bottom
end

#heightInteger

The height of the view.

Returns:

  • (Integer)


30
31
32
# File 'lib/motion-loco/resizable.rb', line 30

def height
  @height
end

#leftInteger

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

Returns:

  • (Integer)


46
47
48
# File 'lib/motion-loco/resizable.rb', line 46

def left
  @left
end

#parent_viewObject Also known as: parentView

Returns the value of attribute parent_view.



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

def parent_view
  @parent_view
end

#rightInteger

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

Returns:

  • (Integer)


62
63
64
# File 'lib/motion-loco/resizable.rb', line 62

def right
  @right
end

#topInteger

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

Returns:

  • (Integer)


78
79
80
# File 'lib/motion-loco/resizable.rb', line 78

def top
  @top
end

#widthInteger

The width of the view.

Returns:

  • (Integer)


93
94
95
# File 'lib/motion-loco/resizable.rb', line 93

def width
  @width
end

Instance Method Details

#initWithFrame(frame) ⇒ Object

Create new instance from a hash of properties with values.

Parameters:

  • frame (Object)

    The CGRect or a Hash of properties.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/motion-loco/resizable.rb', line 108

def initWithFrame(frame)
  if frame.is_a? Hash
    # Set the initial property values from the given hash
    super(CGRect.new)
    frame.each do |key, value|
      self.send("#{key}=", value)
    end
  else
    super(frame)
  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)


124
125
126
127
128
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
# File 'lib/motion-loco/resizable.rb', line 124

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



286
287
288
# File 'lib/motion-loco/resizable.rb', line 286

def view_setup
  viewSetup
end

#viewSetupObject



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

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

#willMoveToSuperview(superview) ⇒ Object

Fires when the superview changes.



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

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