Class: MotionKit::UIViewLayout

Inherits:
Layout show all
Defined in:
lib/motion-kit-ios/layouts/uiview_layout.rb,
lib/motion-kit-ios/layouts/uiview_layout_frame.rb,
lib/motion-kit-ios/layouts/uiview_layout_gradient.rb,
lib/motion-kit-ios/layouts/uiview_layout_constraints.rb,
lib/motion-kit-ios/layouts/uiview_layout_autoresizing.rb

Direct Known Subclasses

UIButtonLayout

Instance Attribute Summary

Attributes inherited from BaseLayout

#parent

Instance Method Summary collapse

Methods inherited from Layout

#add_child, #default_root, #reapply!, #remove_child

Methods inherited from TreeLayout

#add, #all, #build, #built?, #call_style_method, #create, #create_default_root_context, #first, #get, #initial, #last, #nth, #reapply, #reapply!, #remove, #root, #view, view

Methods inherited from BaseLayout

#add_deferred_block, #apply, #apply_with_context, #apply_with_target, #context, #deferred, #deferred_blocks, delegate_method_fix, #initialize, #ipad?, #iphone35?, #iphone4?, #iphone?, method_added, #method_missing, #orientation?, overridden_methods, override_start, override_stop, overrides, #retina?, #run_deferred, #set_layout, #target, #v

Methods included from BaseLayoutClassMethods

#layout_for, #memoize, #new_child, #target_klasses, #targets

Constructor Details

This class inherits a constructor from MotionKit::BaseLayout

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MotionKit::BaseLayout

Instance Method Details

#_calculate_frame(f, from: from_view, relative_to: point) ⇒ Object



93
94
95
96
97
98
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 93

def _calculate_frame(f, from: from_view, relative_to: point)
  if from_view.is_a?(Symbol)
    from_view = self.get(from_view)
  end

  from_view_size = from_view.frame.size
  o = from_view.convertPoint([0, 0], toView: target.superview)

  calculate_view = target

  if point[:x] == :reset || point[:y] == :reset
    calculate_view = UIView.alloc.initWithFrame([[0, 0], target.frame.size])
  end

  if f.is_a?(Hash)
    f = f.merge(relative: true)
  end
  f = MotionKit.calculate(calculate_view, :frame, f, target.superview)
  f.origin.x += o.x
  f.origin.y += o.y

  case point[:x]
  when :min, :reset
    # pass
  when :mid
    f.origin.x += (from_view_size.width - f.size.width) / 2.0
  when :max
    f.origin.x += from_view_size.width - f.size.width
  when :before
    f.origin.x -= f.size.width
  when :after
    f.origin.x += from_view_size.width
  else
    f.origin.x += point[:x]
  end

  case point[:y]
  when :min, :reset
    # pass
  when :mid
    f.origin.y += (from_view_size.height - f.size.height) / 2.0
  when :max
    f.origin.y += from_view_size.height - f.size.height
  when :above
    f.origin.y -= f.size.height
  when :below
    f.origin.y += from_view_size.height
  else
    f.origin.y += point[:y]
  end

  return f
end

#above(from_view, f = {}) ⇒ Object

The first arg can be a view or a frame

Examples:

frame above(view, [[0, 0], [100, 20]])
frame above(:view, x: 0, y: 0, width: 100, height: 20)
frame above(:view, down: 0, right: 0, width: 100, height: 20)


278
279
280
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 278

def above(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :above })
end

#after(from_view, f = {}) ⇒ Object Also known as: right_of

The first arg can be a view or a frame

Examples:

frame after(view, [[0, 0], [100, 20]])
frame after(:view, x: 0, y: 0, width: 100, height: 20)
frame after(:view, down: 0, right: 0, width: 100, height: 20)


306
307
308
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 306

def after(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :after, y: :reset })
end

#autoresizing_mask(*values) ⇒ Object Also known as: autoresizingMask



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/motion-kit-ios/layouts/uiview_layout_autoresizing.rb', line 5

def autoresizing_mask(*values)
  value = 0
  values.each do |mask|
    case mask
    when :flexible_left
      value |= UIViewAutoresizingFlexibleLeftMargin
    when :flexible_width
      value |= UIViewAutoresizingFlexibleWidth
    when :flexible_right
      value |= UIViewAutoresizingFlexibleRightMargin
    when :flexible_top
      value |= UIViewAutoresizingFlexibleTopMargin
    when :flexible_height
      value |= UIViewAutoresizingFlexibleHeight
    when :flexible_bottom
      value |= UIViewAutoresizingFlexibleBottomMargin

    when :rigid_left
      value ^= UIViewAutoresizingFlexibleLeftMargin
    when :rigid_width
      value ^= UIViewAutoresizingFlexibleWidth
    when :rigid_right
      value ^= UIViewAutoresizingFlexibleRightMargin
    when :rigid_top
      value ^= UIViewAutoresizingFlexibleTopMargin
    when :rigid_height
      value ^= UIViewAutoresizingFlexibleHeight
    when :rigid_bottom
      value ^= UIViewAutoresizingFlexibleBottomMargin

    when :fill
      value |= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
    when :fill_top
      value |= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin
    when :fill_bottom
      value |= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin
    when :fill_left
      value |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin
    when :fill_right
      value |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin

    when :pin_to_top_left
      value |= UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin
    when :pin_to_top
      value |= UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin
    when :pin_to_top_right
      value |= UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin
    when :pin_to_left
      value |= UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin
    when :pin_to_center, :pin_to_middle
      value |= UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin
    when :pin_to_right
      value |= UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin
    when :pin_to_bottom_left
      value |= UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin
    when :pin_to_bottom
      value |= UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin
    when :pin_to_bottom_right
      value |= UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
    else
      value |= mask
    end
  end

  target.autoresizingMask = value
  value
end

#before(from_view, f = {}) ⇒ Object Also known as: left_of

The first arg can be a view or a frame

Examples:

frame before(view, [[0, 0], [100, 20]])
frame before(:view, x: 0, y: 0, width: 100, height: 20)
frame before(:view, down: 0, right: 0, width: 100, height: 20)


296
297
298
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 296

def before(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :before, y: :reset })
end

#below(from_view, f = {}) ⇒ Object

The first arg can be a view or a frame

Examples:

frame below(view, [[0, 0], [100, 20]])
frame below(:view, x: 0, y: 0, width: 100, height: 20)
frame below(:view, down: 0, right: 0, width: 100, height: 20)


287
288
289
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 287

def below(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :below })
end

#bottom(value) ⇒ Object



37
38
39
40
41
42
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 37

def bottom(value)
  f = target.frame
  f.origin.y = MotionKit.calculate(target, :height, value) - f.size.height
  target.frame = f
  return CGRectGetMaxY(f)
end

#center(value) ⇒ Object Also known as: middle



75
76
77
78
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 75

def center(value)
  target.center = MotionKit.calculate(target, :center, value)
  return target.center
end

#center_x(value) ⇒ Object Also known as: middle_x



21
22
23
24
25
26
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 21

def center_x(value)
  c = target.center
  c.x = MotionKit.calculate(target, :width, value)
  target.center = c
  return CGRectGetMidX(target.frame)
end

#center_y(value) ⇒ Object Also known as: middle_y



44
45
46
47
48
49
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 44

def center_y(value)
  c = target.center
  c.y = MotionKit.calculate(target, :height, value)
  target.center = c
  return CGRectGetMidY(target.frame)
end

#constraints(view = nil, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/motion-kit-ios/layouts/uiview_layout_constraints.rb', line 5

def constraints(view=nil, &block)
  view ||= target
  if view.is_a?(Symbol)
    view = self.get(view)
  end
  view.setTranslatesAutoresizingMaskIntoConstraints(false)

  deferred do
    constraints_target = ConstraintsTarget.new(view)
    context(constraints_target, &block)
    constraints_target.apply_all_constraints(self, view)
  end
end

#frame(value) ⇒ Object



88
89
90
91
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 88

def frame(value)
  target.frame = MotionKit.calculate(target, :frame, value)
  return target.frame
end

#from_bottom(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_bottom(width: 80, height: 22)
frame from_bottom(another_view, width: 80, height: 22)


249
250
251
252
253
254
255
256
257
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 249

def from_bottom(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :max })
end

#from_bottom_left(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_bottom_left(width: 80, height: 22)
frame from_bottom_left(another_view, width: 80, height: 22)


235
236
237
238
239
240
241
242
243
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 235

def from_bottom_left(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :max })
end

#from_bottom_right(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_bottom_right(width: 80, height: 22)
frame from_bottom_right(another_view, width: 80, height: 22)


263
264
265
266
267
268
269
270
271
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 263

def from_bottom_right(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :max })
end

#from_center(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_center(width: 80, height: 22)
frame from_center(another_view, width: 80, height: 22)


207
208
209
210
211
212
213
214
215
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 207

def from_center(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :mid })
end

#from_left(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_left(width: 80, height: 22)
frame from_left(another_view, width: 80, height: 22)


193
194
195
196
197
198
199
200
201
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 193

def from_left(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :mid })
end

#from_right(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_right(width: 80, height: 22)
frame from_right(another_view, width: 80, height: 22)


221
222
223
224
225
226
227
228
229
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 221

def from_right(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :mid })
end

#from_top(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_top(width: 80, height: 22)
frame from_top(another_view, width: 80, height: 22)


165
166
167
168
169
170
171
172
173
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 165

def from_top(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :min })
end

#from_top_left(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_top_left(width: 80, height: 22)
frame from_top_left(another_view, width: 80, height: 22)


151
152
153
154
155
156
157
158
159
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 151

def from_top_left(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :min })
end

#from_top_right(from_view = nil, f = nil) ⇒ Object

The first arg can be a view or a frame

Examples:

frame from_top_right(width: 80, height: 22)
frame from_top_right(another_view, width: 80, height: 22)


179
180
181
182
183
184
185
186
187
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 179

def from_top_right(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :min })
end

#gradient(&block) ⇒ Object

gradient colors:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/motion-kit-ios/layouts/uiview_layout_gradient.rb', line 6

def gradient(&block)
  gradient_layer = target.motion_kit_meta[:motionkit_gradient_layer] || begin
    gradient_layer = CAGradientLayer.layer
    gradient_layer.frame = CGRect.new([0, 0], target.frame.size)
    target.layer.insertSublayer(gradient_layer, atIndex:0)
    target.motion_kit_meta[:motionkit_gradient_layer] = gradient_layer

    gradient_layer
  end

  context(gradient_layer, &block)

  gradient_layer
end

#height(value) ⇒ Object Also known as: h



60
61
62
63
64
65
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 60

def height(value)
  f = target.frame
  f.size.height = MotionKit.calculate(target, :height, value)
  target.frame = f
  return CGRectGetHeight(f)
end

#origin(value) ⇒ Object



68
69
70
71
72
73
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 68

def origin(value)
  f = target.frame
  f.origin = MotionKit.calculate(target, :origin, value)
  target.frame = f
  return target.frame.origin
end

#relative_to(from_view, f) ⇒ Object

The first arg must be a view

Examples:

frame relative_to(view, [[0, 0], [100, 20]])
frame relative_to(:view, x: 0, y: 0, width: 100, height: 20)
frame relative_to(:view, down: 0, right: 0, width: 100, height: 20)


316
317
318
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 316

def relative_to(from_view, f)
  _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :reset })
end

#right(value) ⇒ Object



14
15
16
17
18
19
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 14

def right(value)
  f = target.frame
  f.origin.x = MotionKit.calculate(target, :width, value) - f.size.width
  target.frame = f
  return CGRectGetMaxX(f)
end

#size(value) ⇒ Object



81
82
83
84
85
86
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 81

def size(value)
  f = target.frame
  f.size = MotionKit.calculate(target, :size, value)
  target.frame = f
  return target.frame.size
end

#width(value) ⇒ Object Also known as: w



52
53
54
55
56
57
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 52

def width(value)
  f = target.frame
  f.size.width = MotionKit.calculate(target, :width, value)
  target.frame = f
  return CGRectGetWidth(f)
end

#x(value) ⇒ Object Also known as: left



6
7
8
9
10
11
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 6

def x(value)
  f = target.frame
  f.origin.x = MotionKit.calculate(target, :width, value)
  target.frame = f
  return CGRectGetMinX(f)
end

#y(value) ⇒ Object Also known as: top



29
30
31
32
33
34
# File 'lib/motion-kit-ios/layouts/uiview_layout_frame.rb', line 29

def y(value)
  f = target.frame
  f.origin.y = MotionKit.calculate(target, :height, value)
  target.frame = f
  return CGRectGetMinY(f)
end