sugarcube 0.11.3 → 0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,11 @@
1
1
  class UITableView
2
2
  class << self
3
3
 
4
- def plain(frame=[[0, 0], [320, 480]])
4
+ def plain(frame=[[0, 0], [0, 0]])
5
5
  UITableView.alloc.initWithFrame(frame, style: :plain.uitableviewstyle)
6
6
  end
7
7
 
8
- def grouped(frame=[[0, 0], [320, 480]])
8
+ def grouped(frame=[[0, 0], [0, 0]])
9
9
  UITableView.alloc.initWithFrame(frame, style: :grouped.uitableviewstyle)
10
10
  end
11
11
 
@@ -22,6 +22,19 @@ class UIView
22
22
  return found
23
23
  end
24
24
 
25
+ # returns the nearest nextResponder instance that is a UIViewController. Goes
26
+ # up the responder chain until the nextResponder is a UIViewController
27
+ # subclass, or returns nil if none is found.
28
+ def controller
29
+ if nextResponder && nextResponder.is_a?(UIViewController)
30
+ nextResponder
31
+ elsif nextResponder
32
+ nextResponder.controller
33
+ else
34
+ nil
35
+ end
36
+ end
37
+
25
38
  def <<(view)
26
39
  self.addSubview view
27
40
  return self
@@ -37,20 +50,10 @@ class UIView
37
50
  self
38
51
  end
39
52
 
40
- def _after_proc(after)
41
- after ? proc { |finished|
42
- if after.arity == 0
43
- after.call
44
- else
45
- after.call(finished)
46
- end
47
- } : nil
48
- end
49
-
50
53
  # If options is a Numeric, it is used as the duration. Otherwise, duration
51
54
  # is an option, and defaults to 0.3. All the transition methods work this
52
55
  # way.
53
- def fade_out(options={}, &after)
56
+ def self.animate(options={}, &animations)
54
57
  if options.is_a? Numeric
55
58
  duration = options
56
59
  options = {}
@@ -58,25 +61,28 @@ class UIView
58
61
  duration = options[:duration] || 0.3
59
62
  end
60
63
 
61
- after = _after_proc(after)
64
+ after_animations = options[:after]
65
+ if after_animations
66
+ if after_animations.arity == 0
67
+ after_adjusted = proc { |finished| after_animations.call }
68
+ else
69
+ after_adjusted = proc { |finished| after_animations.call(finished) }
70
+ end
71
+ else
72
+ after_adjusted = nil
73
+ end
62
74
 
63
- UIView.animateWithDuration(duration,
75
+ UIView.animateWithDuration( duration,
64
76
  delay: options[:delay] || 0,
65
77
  options: options[:options] || UIViewAnimationOptionCurveEaseInOut,
66
- animations: proc{
67
- self.layer.opacity = options[:opacity] || 0
68
-
69
- if assign = options[:assign]
70
- assign.each_pair do |key, value|
71
- self.send("#{key}=", value)
72
- end
73
- end
74
- }, completion:after
78
+ animations: proc,
79
+ completion: after_adjusted
75
80
  )
76
- self
81
+ nil
77
82
  end
78
83
 
79
- def fade_in(options={}, &after)
84
+ # Same as UIView##animate, but acts on self
85
+ def animate(options={}, &animations)
80
86
  if options.is_a? Numeric
81
87
  duration = options
82
88
  options = {}
@@ -84,36 +90,67 @@ class UIView
84
90
  duration = options[:duration] || 0.3
85
91
  end
86
92
 
87
- options[:opacity] = 1.0
88
- fade_out(options, &after)
93
+ assign = options[:assign] || {}
94
+
95
+ UIView.animate(options) {
96
+ animations.call if animations
97
+
98
+ assign.each_pair do |key, value|
99
+ self.send("#{key}=", value)
100
+ end
101
+ }
102
+ self
103
+ end
104
+
105
+ # Changes the layer opacity.
106
+ def fade(options={}, &after)
107
+ if options.is_a? Numeric
108
+ options = { opacity: options }
109
+ end
110
+
111
+ options[:after] ||= after
112
+
113
+ animate(options) {
114
+ self.layer.opacity = options[:opacity]
115
+ }
116
+ end
117
+
118
+ # Changes the layer opacity to 0.
119
+ # @see #fade
120
+ def fade_out(options={}, &after)
121
+ if options.is_a? Numeric
122
+ options = { duration: options }
123
+ end
124
+
125
+ options[:opacity] ||= 0.0
126
+
127
+ fade(options, &after)
128
+ end
129
+
130
+ # Changes the layer opacity to 1.
131
+ # @see #fade
132
+ def fade_in(options={}, &after)
133
+ if options.is_a? Numeric
134
+ options = { duration: options }
135
+ end
136
+
137
+ options[:opacity] ||= 1.0
138
+
139
+ fade(options, &after)
89
140
  end
90
141
 
91
142
  def move_to(position, options={}, &after)
92
143
  if options.is_a? Numeric
93
- duration = options
94
- options = {}
95
- else
96
- duration = options[:duration] || 0.3
144
+ options = { duration: options }
97
145
  end
98
146
 
99
- after = _after_proc(after)
147
+ options[:after] ||= after
100
148
 
101
- UIView.animateWithDuration(duration,
102
- delay: options[:delay] || 0,
103
- options: options[:options] || UIViewAnimationOptionCurveEaseInOut,
104
- animations: proc{
105
- f = self.frame
106
- f.origin = SugarCube::CoreGraphics::Point(position)
107
- self.frame = f
108
-
109
- if assign = options[:assign]
110
- assign.each_pair do |key, value|
111
- self.send("#{key}=", value)
112
- end
113
- end
114
- }, completion:after
115
- )
116
- self
149
+ animate(options) {
150
+ f = self.frame
151
+ f.origin = SugarCube::CoreGraphics::Point(position)
152
+ self.frame = f
153
+ }
117
154
  end
118
155
 
119
156
  def delta_to(delta, options={}, &after)
@@ -158,12 +195,16 @@ class UIView
158
195
 
159
196
  offset = options[:offset] || 8
160
197
  repeat = options[:repeat] || 3
161
- duration /= repeat
198
+ if repeat == Float::INFINITY
199
+ duration = 0.1
200
+ else
201
+ duration /= repeat
202
+ end
162
203
  keypath = options[:keypath] || 'transform.translation.x'
163
204
 
164
- origin = 0
165
- left = -offset
166
- right = +offset
205
+ origin = options[:origin] || 0
206
+ left = origin - offset
207
+ right = origin + offset
167
208
 
168
209
  animation = CAKeyframeAnimation.animationWithKeyPath(keypath)
169
210
  animation.duration = duration
@@ -2,6 +2,7 @@ class UIViewController
2
2
 
3
3
  def push(view_controller)
4
4
  self.addChildViewController(view_controller)
5
+ view_controller.didMoveToParentViewController(self)
5
6
  self
6
7
  end
7
8
 
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.11.3'
2
+ Version = '0.12'
3
3
  end
@@ -0,0 +1,304 @@
1
+ describe "SugarCube::CoreGraphics" do
2
+ describe 'Point()' do
3
+ it 'should accept 1 argument: a CGPoint' do
4
+ cgpoint = CGPoint.new(1, 2)
5
+ p = Point(cgpoint)
6
+ p.x.should == 1
7
+ p.y.should == 2
8
+ CGPointEqualToPoint(p, cgpoint).should == true
9
+ end
10
+
11
+ it 'should accept 1 argument: a CGSize' do
12
+ cgsize = CGSize.new(1, 2)
13
+ p = Point(cgsize)
14
+ p.x.should == 1
15
+ p.y.should == 2
16
+ CGPointEqualToPoint(p, CGPoint.new(cgsize.width, cgsize.height)).should == true
17
+ end
18
+
19
+ it 'should accept 1 argument: a UIOffset' do
20
+ uioffset = UIOffset.new(1, 2)
21
+ p = Point(uioffset)
22
+ p.x.should == 1
23
+ p.y.should == 2
24
+ CGPointEqualToPoint(p, CGPoint.new(uioffset.horizontal, uioffset.vertical)).should == true
25
+ end
26
+
27
+ it 'should accept 1 argument: an Array' do
28
+ array = [1, 2]
29
+ p = Point(array)
30
+ p.x.should == 1
31
+ p.y.should == 2
32
+ CGPointEqualToPoint(p, CGPoint.new(array[0], array[1])).should == true
33
+ end
34
+
35
+ it 'should accept 2 arguments: two numbers' do
36
+ args = [1, 2]
37
+ p = Point(*args)
38
+ p.x.should == 1
39
+ p.y.should == 2
40
+ CGPointEqualToPoint(p, CGPoint.new(args[0], args[1])).should == true
41
+ end
42
+ end
43
+
44
+ describe 'Size()' do
45
+ it 'should accept 1 argument: a CGPoint' do
46
+ cgpoint = CGPoint.new(1, 2)
47
+ s = Size(cgpoint)
48
+ s.width.should == 1
49
+ s.height.should == 2
50
+ CGSizeEqualToSize(s, CGSize.new(cgpoint.x, cgpoint.y)).should == true
51
+ end
52
+
53
+ it 'should accept 1 argument: a CGSize' do
54
+ cgsize = CGSize.new(1, 2)
55
+ s = Size(cgsize)
56
+ s.width.should == 1
57
+ s.height.should == 2
58
+ CGSizeEqualToSize(s, cgsize).should == true
59
+ end
60
+
61
+ it 'should accept 1 argument: a UIOffset' do
62
+ uioffset = UIOffset.new(1, 2)
63
+ s = Size(uioffset)
64
+ s.width.should == 1
65
+ s.height.should == 2
66
+ CGSizeEqualToSize(s, CGSize.new(uioffset.horizontal, uioffset.vertical)).should == true
67
+ end
68
+
69
+ it 'should accept 1 argument: an Array' do
70
+ array = [1, 2]
71
+ s = Size(array)
72
+ s.width.should == 1
73
+ s.height.should == 2
74
+ CGSizeEqualToSize(s, CGSize.new(array[0], array[1])).should == true
75
+ end
76
+
77
+ it 'should accept 2 arguments: two numbers' do
78
+ args = [1, 2]
79
+ s = Size(*args)
80
+ s.width.should == 1
81
+ s.height.should == 2
82
+ CGSizeEqualToSize(s, CGSize.new(args[0], args[1])).should == true
83
+ end
84
+ end
85
+
86
+ describe 'Rect()' do
87
+ it 'should accept 1 argument: a CGRect' do
88
+ cgr = CGRect.new([1, 2], [3, 4])
89
+ r = Rect(cgr)
90
+ r.origin.x.should == 1
91
+ r.origin.y.should == 2
92
+ r.size.width.should == 3
93
+ r.size.height.should == 4
94
+ CGRectEqualToRect(r, cgr).should == true
95
+ end
96
+
97
+ it 'should accept 1 argument: a UIView' do
98
+ uiview = UIView.alloc.initWithFrame([[1, 2], [3, 4]])
99
+ r = Rect(uiview)
100
+ r.origin.x.should == 1
101
+ r.origin.y.should == 2
102
+ r.size.width.should == 3
103
+ r.size.height.should == 4
104
+ CGRectEqualToRect(r, uiview.frame).should == true
105
+ end
106
+
107
+ it 'should accept 1 argument: a CALayer' do
108
+ calayer = CALayer.alloc.init
109
+ calayer.frame = [[1, 2], [3, 4]]
110
+ r = Rect(calayer)
111
+ r.origin.x.should == 1
112
+ r.origin.y.should == 2
113
+ r.size.width.should == 3
114
+ r.size.height.should == 4
115
+ CGRectEqualToRect(r, calayer.frame).should == true
116
+ end
117
+
118
+ it 'should accept 1 argument: an Array of 2 arrays' do
119
+ array = [[1, 2], [3, 4]]
120
+ r = Rect(array)
121
+ r.origin.x.should == 1
122
+ r.origin.y.should == 2
123
+ r.size.width.should == 3
124
+ r.size.height.should == 4
125
+ CGRectEqualToRect(r, CGRect.new(array[0], array[1])).should == true
126
+ end
127
+
128
+ it 'should accept 1 argument: an Array of 4 numbers' do
129
+ array = [1, 2, 3, 4]
130
+ r = Rect(array)
131
+ r.origin.x.should == 1
132
+ r.origin.y.should == 2
133
+ r.size.width.should == 3
134
+ r.size.height.should == 4
135
+ CGRectEqualToRect(r, CGRect.new([array[0], array[1]], [array[2], array[3]])).should == true
136
+ end
137
+
138
+ it 'should accept 2 arguments (CGPoint, CGSize)' do
139
+ args = [CGPoint.new(1, 2), CGSize.new(3, 4)]
140
+ r = Rect(*args)
141
+ r.origin.x.should == 1
142
+ r.origin.y.should == 2
143
+ r.size.width.should == 3
144
+ r.size.height.should == 4
145
+ CGRectEqualToRect(r, CGRect.new(args[0], args[1])).should == true
146
+ end
147
+
148
+ it 'should accept 2 arguments: (CGPoint, CGSize)' do
149
+ args = [CGPoint.new(1, 2), CGSize.new(3, 4)]
150
+ r = Rect(*args)
151
+ r.origin.x.should == 1
152
+ r.origin.y.should == 2
153
+ r.size.width.should == 3
154
+ r.size.height.should == 4
155
+ CGRectEqualToRect(r, CGRect.new(args[0], args[1])).should == true
156
+ end
157
+
158
+ it 'should accept 3 arguments: ([x, y], w, h)' do
159
+ args = [[1, 2], 3, 4]
160
+ r = Rect(*args)
161
+ r.origin.x.should == 1
162
+ r.origin.y.should == 2
163
+ r.size.width.should == 3
164
+ r.size.height.should == 4
165
+ CGRectEqualToRect(r, CGRect.new(args[0], [args[1], args[2]])).should == true
166
+ end
167
+
168
+ it 'should accept 3 arguments: (CGPoint, w, h)' do
169
+ args = [CGPoint.new(1, 2), 3, 4]
170
+ r = Rect(*args)
171
+ r.origin.x.should == 1
172
+ r.origin.y.should == 2
173
+ r.size.width.should == 3
174
+ r.size.height.should == 4
175
+ CGRectEqualToRect(r, CGRect.new(args[0], [args[1], args[2]])).should == true
176
+ end
177
+
178
+ it 'should accept 3 arguments: (x, y, [w, h])' do
179
+ args = [1, 2, [3, 4]]
180
+ r = Rect(*args)
181
+ r.origin.x.should == 1
182
+ r.origin.y.should == 2
183
+ r.size.width.should == 3
184
+ r.size.height.should == 4
185
+ CGRectEqualToRect(r, CGRect.new([args[0], args[1]], args[2])).should == true
186
+ end
187
+
188
+ it 'should accept 3 arguments: (x, y, CGSize)' do
189
+ args = [1, 2, CGSize.new(3, 4)]
190
+ r = Rect(*args)
191
+ r.origin.x.should == 1
192
+ r.origin.y.should == 2
193
+ r.size.width.should == 3
194
+ r.size.height.should == 4
195
+ CGRectEqualToRect(r, CGRect.new([args[0], args[1]], args[2])).should == true
196
+ end
197
+
198
+ it 'should accept 4 arguments: 4 numbers' do
199
+ args = [1, 2, 3, 4]
200
+ r = Rect(*args)
201
+ r.origin.x.should == 1
202
+ r.origin.y.should == 2
203
+ r.size.width.should == 3
204
+ r.size.height.should == 4
205
+ CGRectEqualToRect(r, CGRect.new([args[0], args[1]], [args[2], args[3]])).should == true
206
+ end
207
+ end
208
+
209
+ describe 'Offset()' do
210
+ it 'should accept 1 argument: a CGPoint' do
211
+ cgpoint = CGPoint.new(1, 2)
212
+ o = Offset(cgpoint)
213
+ o.horizontal.should == 1
214
+ o.vertical.should == 2
215
+ UIOffsetEqualToOffset(o, UIOffset.new(cgpoint.x, cgpoint.y)).should == true
216
+ end
217
+
218
+ it 'should accept 1 argument: a CGSize' do
219
+ cgsize = CGSize.new(1, 2)
220
+ o = Offset(cgsize)
221
+ o.horizontal.should == 1
222
+ o.vertical.should == 2
223
+ UIOffsetEqualToOffset(o, UIOffset.new(cgsize.width, cgsize.height)).should == true
224
+ end
225
+
226
+ it 'should accept 1 argument: a UIOffset' do
227
+ uioffset = UIOffset.new(1, 2)
228
+ o = Offset(uioffset)
229
+ o.horizontal.should == 1
230
+ o.vertical.should == 2
231
+ UIOffsetEqualToOffset(o, uioffset).should == true
232
+ end
233
+
234
+ it 'should accept 1 argument: a Numeric' do
235
+ numeric = 5
236
+ o = Offset(numeric)
237
+ o.horizontal.should == 5
238
+ o.vertical.should == 5
239
+ UIOffsetEqualToOffset(o, UIOffset.new(numeric, numeric)).should == true
240
+ end
241
+ end
242
+
243
+ describe 'EdgeInsets()' do
244
+ it 'should accept 1 argument: a CGRect' do
245
+ cgrect = CGRect.new([1, 2], [3, 4])
246
+ e = EdgeInsets(cgrect)
247
+ e.left.should == 1
248
+ e.top.should == 2
249
+ e.right.should == 3
250
+ e.bottom.should == 4
251
+ UIEdgeInsetsEqualToEdgeInsets(e, UIEdgeInsets.new(cgrect.origin.y, cgrect.origin.x, cgrect.size.height, cgrect.size.width)).should == true
252
+ end
253
+
254
+ it 'should accept 1 argument: a UIEdgeInset' do
255
+ uiedgeinsets = UIEdgeInsets.new(1, 2, 3, 4)
256
+ e = EdgeInsets(uiedgeinsets)
257
+ e.top.should == 1
258
+ e.left.should == 2
259
+ e.bottom.should == 3
260
+ e.right.should == 4
261
+ UIEdgeInsetsEqualToEdgeInsets(e, uiedgeinsets).should == true
262
+ end
263
+
264
+ it 'should accept 1 argument: a Numeric' do
265
+ numeric = 5
266
+ e = EdgeInsets(numeric)
267
+ e.top.should == 5
268
+ e.left.should == 5
269
+ e.bottom.should == 5
270
+ e.right.should == 5
271
+ UIEdgeInsetsEqualToEdgeInsets(e, UIEdgeInsets.new(numeric, numeric, numeric, numeric)).should == true
272
+ end
273
+
274
+ it 'should accept 2 arguments: Numerics' do
275
+ args = [5, 6]
276
+ e = EdgeInsets(*args)
277
+ e.top.should == 5
278
+ e.left.should == 6
279
+ e.bottom.should == 5
280
+ e.right.should == 6
281
+ UIEdgeInsetsEqualToEdgeInsets(e, UIEdgeInsets.new(args[0], args[1], args[0], args[1])).should == true
282
+ end
283
+
284
+ it 'should accept 3 arguments: Numerics' do
285
+ args = [5, 6, 7]
286
+ e = EdgeInsets(*args)
287
+ e.top.should == 5
288
+ e.left.should == 6
289
+ e.bottom.should == 7
290
+ e.right.should == 6
291
+ UIEdgeInsetsEqualToEdgeInsets(e, UIEdgeInsets.new(args[0], args[1], args[2], args[1])).should == true
292
+ end
293
+
294
+ it 'should accept 4 arguments: Numerics' do
295
+ args = [5, 6, 7, 8]
296
+ e = EdgeInsets(*args)
297
+ e.top.should == 5
298
+ e.left.should == 6
299
+ e.bottom.should == 7
300
+ e.right.should == 8
301
+ UIEdgeInsetsEqualToEdgeInsets(e, UIEdgeInsets.new(args[0], args[1], args[2], args[3])).should == true
302
+ end
303
+ end
304
+ end