Class: MotionKit::CALayerHelpers

Inherits:
TreeLayout show all
Defined in:
lib/motion-kit-cocoa/helpers/calayer_helpers.rb,
lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb

Direct Known Subclasses

CAGradientLayerHelpers, CALayerLayout

Instance Attribute Summary

Attributes inherited from BaseLayout

#parent

Instance Method Summary collapse

Methods inherited from TreeLayout

#add, #all, #all_views, #always, #build, #built?, #child_layouts, #create, #create_default_root_context, #first, #forget, #forget_tree, #forget_view, #get, #get_view, #initial, #initial?, #initialize, #last, #last_view, #name_element, #nearest, #next, #nth, #nth_view, #prev, #reapply, #reapply!, #reapply?, #reapply_blocks, #remove, #remove_view, #root, #run_reapply_blocks, view, #view

Methods inherited from BaseLayout

#add_deferred_block, #apply, #apply_with_context, #apply_with_target, #context, #deferred, #deferred_blocks, delegate_method_fix, #has_context?, #initialize, #ipad?, #iphone35?, #iphone47?, #iphone4?, #iphone55?, #iphone?, #is_parent_layout?, #method_missing, #objc_version, #orientation?, #orientation_block, #parent_layout, #retina?, #ruby_version, #run_deferred, #set_parent_layout, #target, #tv?, #v

Methods included from BaseLayoutClassMethods

#layout_for, #memoize, #target_klasses, #targets

Constructor Details

This class inherits a constructor from MotionKit::TreeLayout

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_layer, relative_to: point) ⇒ Object



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
146
147
148
149
150
151
152
153
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 104

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

  from_layer_size = from_layer.frame.size
  o = from_layer.convertPoint([0, 0], toLayer: target.superlayer)

  calculate_layer = CALayer.layer
  calculate_layer.frame = [[0, 0], target.frame.size]

  if f.is_a?(Hash)
    f = f.merge(relative: true)
  end
  f = MotionKit.calculate(calculate_layer, :frame, f, target.superlayer || parent_layout.parent)
  f.origin.x += o.x
  f.origin.y += o.y

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

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

  return f
end

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

The first arg can be a layer or a frame

Examples:

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


286
287
288
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 286

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

#add_child(subview) ⇒ Object



12
13
14
# File 'lib/motion-kit-cocoa/helpers/calayer_helpers.rb', line 12

def add_child(subview)
  target.addSublayer(subview)
end

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

The first arg can be a layer or a frame

Examples:

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


314
315
316
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 314

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

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

The first arg can be a layer or a frame

Examples:

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


304
305
306
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 304

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

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

The first arg can be a layer or a frame

Examples:

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


295
296
297
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 295

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

#bottom(value) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 40

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

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



83
84
85
86
87
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 83

def center(value)
  superview = target.superlayer || parent_layout.parent
  target.center = MotionKit.calculate(target, :center, value, superview)
  return target.center
end

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



22
23
24
25
26
27
28
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 22

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

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



48
49
50
51
52
53
54
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 48

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

#default_rootObject

platform specific default root view



8
9
10
# File 'lib/motion-kit-cocoa/helpers/calayer_helpers.rb', line 8

def default_root
  self.class.targets.layer
end

#frame(value) ⇒ Object



98
99
100
101
102
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 98

def frame(value)
  superview = target.superlayer || parent_layout.parent
  target.frame = MotionKit.calculate(target, :frame, value, superview)
  return target.frame
end

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

The first arg can be a layer or a frame

Examples:

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


257
258
259
260
261
262
263
264
265
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 257

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

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

The first arg can be a layer or a frame

Examples:

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


243
244
245
246
247
248
249
250
251
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 243

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

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

The first arg can be a layer or a frame

Examples:

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


271
272
273
274
275
276
277
278
279
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 271

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

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

The first arg can be a layer or a frame

Examples:

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


215
216
217
218
219
220
221
222
223
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 215

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

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

The first arg can be a layer or a frame

Examples:

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


201
202
203
204
205
206
207
208
209
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 201

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

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

The first arg can be a layer or a frame

Examples:

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


229
230
231
232
233
234
235
236
237
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 229

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

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

The first arg can be a layer or a frame

Examples:

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


173
174
175
176
177
178
179
180
181
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 173

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

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

The first arg can be a layer or a frame

Examples:

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


159
160
161
162
163
164
165
166
167
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 159

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

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

The first arg can be a layer or a frame

Examples:

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


187
188
189
190
191
192
193
194
195
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 187

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

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



66
67
68
69
70
71
72
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 66

def height(value)
  f = target.frame
  superview = target.superlayer || parent_layout.parent
  f.size.height = MotionKit.calculate(target, :height, value, superview)
  target.frame = f
  return CGRectGetHeight(f)
end

#layerObject

a more appropriate name for the root layer



21
22
23
# File 'lib/motion-kit-cocoa/helpers/calayer_helpers.rb', line 21

def layer
  self.view
end

#origin(value) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 75

def origin(value)
  f = target.frame
  superview = target.superlayer || parent_layout.parent
  f.origin = MotionKit.calculate(target, :origin, value, superview)
  target.frame = f
  return target.frame.origin
end

#relative_to(from_layer, f) ⇒ Object

The first arg must be a layer

Examples:

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


324
325
326
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 324

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

#remove_child(subview) ⇒ Object



16
17
18
# File 'lib/motion-kit-cocoa/helpers/calayer_helpers.rb', line 16

def remove_child(subview)
  subview.removeFromSuperlayer
end

#right(value) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 14

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

#size(value) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 90

def size(value)
  f = target.frame
  superview = target.superlayer || parent_layout.parent
  f.size = MotionKit.calculate(target, :size, value, superview)
  target.frame = f
  return target.frame.size
end

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



57
58
59
60
61
62
63
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 57

def width(value)
  f = target.frame
  superview = target.superlayer || parent_layout.parent
  f.size.width = MotionKit.calculate(target, :width, value, superview)
  target.frame = f
  return CGRectGetWidth(f)
end

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



5
6
7
8
9
10
11
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 5

def x(value)
  f = target.frame
  superview = target.superlayer || parent_layout.parent
  f.origin.x = MotionKit.calculate(target, :width, value, superview)
  target.frame = f
  return CGRectGetMinX(f)
end

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



31
32
33
34
35
36
37
# File 'lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb', line 31

def y(value)
  f = target.frame
  superview = target.superlayer || parent_layout.parent
  f.origin.y = MotionKit.calculate(target, :height, value, superview)
  target.frame = f
  return CGRectGetMinY(f)
end