sugarcube 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  sugarcube
2
2
  =========
3
3
 
4
- Some sugar for your cocoa, or your tea.
4
+ Some sugar for your cocoa, or your [tea][sweettea].
5
5
 
6
6
  About
7
7
  -----
8
8
 
9
9
  CocoaTouch/iOS is a *verbose* framework. These extensions hope to make
10
10
  development in rubymotion more enjoyable by tacking "UI" methods onto the base
11
- classes (String, Fixnum, Float). With sugarcube, you can create a color from an
11
+ classes (String, Fixnum, Numeric). With sugarcube, you can create a color from an
12
12
  integer or symbol, or create a UIFont or UIImage from a string.
13
13
 
14
14
  Some UI classes are opened up as well, like adding the '<<' operator to a UIView
@@ -52,19 +52,19 @@ Examples
52
52
  # create a UIColor from a hex value
53
53
  0xffffff.uicolor # => UIColor.colorWithRed(1.0, green:1.0, blue:1.0, alpha:1.0)
54
54
  0xffffff.uicolor(0.5) # => UIColor.colorWithRed(1.0, green:1.0, blue:1.0, alpha:0.5)
55
-
56
- # create a percentage
57
- 100.percent # => 1.00
58
- 55.percent # => 0.55
59
55
  ```
60
56
 
61
- Float
62
- -------
57
+ Numeric
58
+ ---------
63
59
 
64
60
  ```ruby
65
61
  # create a percentage
66
62
  100.0.percent # => 1.00
67
63
  55.0.percent # => 0.55
64
+
65
+ 1.3.seconds.later do
66
+ @someview.fade_out
67
+ end
68
68
  ```
69
69
 
70
70
  NSURL
@@ -129,6 +129,7 @@ This is the "big daddy". Lots of sugar here...
129
129
  :small.uifontsize # => UIFont.smallSystemFontSize
130
130
  :small.uifont # => UIFont.systemFontOfSize(:small.uifontsize)
131
131
  :bold.uifont(:small) # UIFont.boldSystemFontOfSize(:small.uifontsize)
132
+ :large.uiactivityindicatorstyle # :large, :white, :gray
132
133
  ```
133
134
 
134
135
  UIImage
@@ -144,6 +145,8 @@ image.uicolor # => UIColor.colorWithPatternImage(image)
144
145
 
145
146
  ```ruby
146
147
  self.view << subview # => self.view.addSubview(subview)
148
+ self.view.show # => self.hidden = false
149
+ self.view.hide # => self.hidden = true
147
150
  ```
148
151
 
149
152
  UIControl
@@ -169,6 +172,22 @@ button.off(:all)
169
172
  You can only remove handlers by "type", not by the action. e.g. If you bind
170
173
  three `:touch` events, calling `button.off(:touch)` will remove all three.
171
174
 
175
+ UINavigationController
176
+ ------------------------
177
+
178
+ `push`/`<<` and `pop` instead of `pushViewController` and `popViewController`.
179
+ `!` and `!(view)` instead of `popToRootViewController` and `popToViewController`
180
+
181
+ animated is `true` for all these.
182
+
183
+ ```ruby
184
+ nav_ctlr.push(new_ctlr)
185
+ nav_ctlr << new_ctlr
186
+ nav_ctlr.pop
187
+ nav_ctlr.!
188
+ nav_ctlr.!(another_view_ctlr)
189
+ ```
190
+
172
191
  NSNotificationCenter
173
192
  ----------------------
174
193
 
@@ -179,12 +198,25 @@ Makes it easy to post a notification to some or all objects.
179
198
  "my notification".post_notification # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:nil)
180
199
  "my notification".post_notification(obj) # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj)
181
200
  "my notification".post_notification(obj, user: 'dict') # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj, userInfo:{user: 'dict'})
201
+ ```
202
+
203
+ NSTimer
204
+ ---------
205
+
206
+ ```ruby
207
+ 1.second.later do
208
+ @view.shake
209
+ end
182
210
  ```
183
211
 
184
212
  NSUserDefaults
185
213
  ----------------
186
214
 
187
215
  ```ruby
216
+ 'key'.set_default(['any', 'objects']) # => NSUserDefaults.standardUserDefaults.setObject(['any', 'objects'], forKey: :key)
217
+ 'key'.get_default # => NSUserDefaults.standardUserDefaults.objectForKey(:key)
218
+
219
+ # symbols are converted to strings, so theses are equivalent
188
220
  :key.set_default(['any', 'objects']) # => NSUserDefaults.standardUserDefaults.setObject(['any', 'objects'], forKey: :key)
189
221
  :key.get_default # => NSUserDefaults.standardUserDefaults.objectForKey(:key)
190
222
  ```
@@ -255,8 +287,23 @@ view.fade_out(0.5, delay: 0,
255
287
  opacity: 0.5) { |view|
256
288
  view.removeFromSuperview
257
289
  }
290
+
258
291
  view.move_to([0, 100]) # move to position 0, 100
259
292
  view.delta_to([0, 100]) # move over 0, down 100, from current position
293
+
294
+ view.slide :left # slides the entire view one "page" to the left, right, up, or down
295
+
296
+ view.shake # shakes the view.
297
+ # options w/ default values:
298
+ shake offset: 8, # move 8 px left, and 8 px right
299
+ repeat: 3, # three times
300
+ duration: 0.3, # for a total of 0.3 seconds
301
+ keypath: 'transform.translate.x'
302
+
303
+ # vigorous nodding - modifying transform.translation.y:
304
+ view.shake offset: 20, repeat: 10, duration: 5, keypath: 'transform.translation.y'
305
+ # an adorable wiggle - modifying transform.rotation:
306
+ superview.shake offset: 0.1, repeat: 2, duration: 0.5, keypath: 'transform.rotation'
260
307
  ```
261
308
 
262
309
  REPL View adjustments
@@ -284,7 +331,8 @@ Assume I ran `include SugarCube::Adjust` in these examples.
284
331
  > taller 1
285
332
  > shorter 1
286
333
  > size 100, 10 # set size to width:100, height: 10
287
- > restore # original frame is saved when you call adjust
334
+ > shadow(opacity: 0.5, offset: [0, 0], color: :black, radius: 1) # and path, which is a CGPath object.
335
+ > restore # original frame and shadow is saved when you call `adjust`
288
336
  ```
289
337
 
290
338
  ```ruby
@@ -300,7 +348,15 @@ Assume I ran `include SugarCube::Adjust` in these examples.
300
348
  > t # taller
301
349
  > s # shorter
302
350
  > z 100, 10 # size, also accepts an array (or Size() object)
303
- > r # restore
351
+ > # you can also query your view. You will get nice-looking
352
+ > # SugarCube::CoreGraphics objects
353
+ > f # frame
354
+ [[0, 0], [320, 480]]
355
+ > o # origin
356
+ [0, 0]
357
+ > z # size
358
+ [320, 480]
359
+ > h # shadow - this returns an array identical to what you can pass to `shadow`
304
360
 
305
361
  # if you forget what view you are adjusting, run `adjust` again
306
362
  > a
@@ -308,3 +364,4 @@ Assume I ran `include SugarCube::Adjust` in these examples.
308
364
  ```
309
365
 
310
366
  [BubbleWrap]: https://github.com/rubymotion/BubbleWrap
367
+ [sweettea]: https://github.com/colinta/sweettea/README.md
@@ -1,10 +1,23 @@
1
1
  unless defined?(Motion::Project::Config)
2
- raise "sugarcube must be required within a RubyMotion project Rakefile."
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
3
  end
4
4
 
5
5
 
6
6
  Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = 0
11
+ app.files.each_index do |index|
12
+ file = app.files[index]
13
+ if file =~ /^(?:\.\/)?app\//
14
+ # found app/, so stop looking
15
+ break
16
+ end
17
+ insert_point = index + 1
18
+ end
19
+
7
20
  Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube/**/*.rb')).reverse.each do |file|
8
- app.files.unshift(file)
21
+ app.files.insert(insert_point, file)
9
22
  end
10
23
  end
@@ -7,11 +7,26 @@ module SugarCube
7
7
  return @@sugarcube_view if not view
8
8
 
9
9
  @@sugarcube_view = view
10
- @@sugarcube_restore = view.frame
10
+ @@sugarcube_restore = {
11
+ frame: SugarCube::Adjust.frame,
12
+ shadow: SugarCube::Adjust.shadow,
13
+ }
14
+
11
15
  view
12
16
  end
13
17
  alias :a :adjust
14
18
 
19
+ ##| FRAME
20
+ def frame f=nil
21
+ @@sugarcube_view ||= nil
22
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
23
+
24
+ return SugarCube::CoreGraphics::Rect(@@sugarcube_view.frame) if not f
25
+
26
+ @@sugarcube_view.frame = f
27
+ end
28
+ alias :f :frame
29
+
15
30
  ##| ORIGIN
16
31
  def left val=1
17
32
  SugarCube::right -val
@@ -19,6 +34,9 @@ module SugarCube
19
34
  alias :l :left
20
35
 
21
36
  def right val=1
37
+ @@sugarcube_view ||= nil
38
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
39
+
22
40
  f = @@sugarcube_view.frame
23
41
  f.origin.x += val
24
42
  @@sugarcube_view.frame = f
@@ -31,6 +49,9 @@ module SugarCube
31
49
  alias :u :up
32
50
 
33
51
  def down val=1
52
+ @@sugarcube_view ||= nil
53
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
54
+
34
55
  f = @@sugarcube_view.frame
35
56
  f.origin.y += val
36
57
  @@sugarcube_view.frame = f
@@ -38,6 +59,9 @@ module SugarCube
38
59
  alias :d :down
39
60
 
40
61
  def origin x=nil, y=nil
62
+ @@sugarcube_view ||= nil
63
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
64
+
41
65
  f = @@sugarcube_view.frame
42
66
  return SugarCube::CoreGraphics::Point(f.origin) if not x
43
67
 
@@ -58,6 +82,9 @@ module SugarCube
58
82
  alias :n :thinner
59
83
 
60
84
  def wider val=1
85
+ @@sugarcube_view ||= nil
86
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
87
+
61
88
  f = @@sugarcube_view.frame
62
89
  f.size.width += val
63
90
  @@sugarcube_view.frame = f
@@ -70,6 +97,9 @@ module SugarCube
70
97
  alias :s :shorter
71
98
 
72
99
  def taller val=1
100
+ @@sugarcube_view ||= nil
101
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
102
+
73
103
  f = @@sugarcube_view.frame
74
104
  f.size.height += val
75
105
  @@sugarcube_view.frame = f
@@ -77,6 +107,9 @@ module SugarCube
77
107
  alias :t :taller
78
108
 
79
109
  def size w=nil, h=nil
110
+ @@sugarcube_view ||= nil
111
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
112
+
80
113
  f = @@sugarcube_view.frame
81
114
  return SugarCube::CoreGraphics::Size(f.size) if not w
82
115
 
@@ -90,11 +123,50 @@ module SugarCube
90
123
  end
91
124
  alias :z :size
92
125
 
126
+ ##| SHADOW
127
+ def shadow shadow=nil
128
+ @@sugarcube_view ||= nil
129
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
130
+
131
+ if shadow
132
+ {
133
+ opacity: :'shadowOpacity=',
134
+ radius: :'shadowRadius=',
135
+ offset: :'shadowOffset=',
136
+ color: :'shadowColor=',
137
+ path: :'shadowPath=',
138
+ }.each { |key, msg|
139
+ if value = shadow[key]
140
+ if key == :color and [Symbol, Fixnum, NSString, UIImage, UIColor].any?{|klass| value.is_a? klass}
141
+ value = value.uicolor.CGColor
142
+ end
143
+ @@sugarcube_view.layer.send(msg, value)
144
+ @@sugarcube_view.layer.masksToBounds = false
145
+ @@sugarcube_view.layer.shouldRasterize = true
146
+ end
147
+ }
148
+ @@sugarcube_view
149
+ else
150
+ {
151
+ opacity: @@sugarcube_view.layer.shadowOpacity,
152
+ radius: @@sugarcube_view.layer.shadowRadius,
153
+ offset: @@sugarcube_view.layer.shadowOffset,
154
+ color: @@sugarcube_view.layer.shadowColor,
155
+ path: @@sugarcube_view.layer.shadowPath,
156
+ }
157
+ end
158
+ end
159
+ alias :h :shadow
160
+
93
161
  ##| RESTORE
94
162
  def restore
95
- @@sugarcube_view.frame = @@sugarcube_restore
163
+ @@sugarcube_view ||= nil
164
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
165
+
166
+ @@sugarcube_restore.each do |msg, value|
167
+ SugarCube::Adjust.send(msg, value)
168
+ end
96
169
  end
97
- alias :r :restore
98
170
 
99
171
  end
100
172
  end
@@ -31,6 +31,29 @@ class CGRectArray < Array
31
31
  self[1] = SugarCube::CoreGraphics::Size(val)
32
32
  end
33
33
 
34
+ # returns an intersection of self and rect, or moves the Rect using Point,
35
+ # or increases the size using Size
36
+ def +(rect)
37
+ case rect
38
+ when CGRectArray
39
+ x1 = self.origin.x < rect.origin.x ? self.origin.x : rect.origin.x
40
+ y1 = self.origin.y < rect.origin.y ? self.origin.y : rect.origin.y
41
+ x2 = self.origin.x + self.size.width > rect.origin.x + rect.size.width ? self.origin.x + self.size.width : rect.origin.x + rect.size.width
42
+ y2 = self.origin.y + self.size.height > rect.origin.y + rect.size.height ? self.origin.y + self.size.height : rect.origin.y + rect.size.height
43
+ SugarCube::CoreGraphics::Rect(x1, y1, x2-x1, y2-y1)
44
+ when CGPointArray
45
+ x = self.origin.x + rect.x
46
+ y = self.origin.y + rect.y
47
+ SugarCube::CoreGraphics::Rect([[x, y], self.size])
48
+ when CGSizeArray
49
+ w = self.size.width + rect.width
50
+ h = self.size.height + rect.height
51
+ SugarCube::CoreGraphics::Rect([self.origin, [w, h]])
52
+ else
53
+ super
54
+ end
55
+ end
56
+
34
57
  end
35
58
 
36
59
 
@@ -52,6 +75,20 @@ class CGPointArray < Array
52
75
  self[1] = val
53
76
  end
54
77
 
78
+ # adds a vector to this point, or creates a Rect by adding a size
79
+ def +(point)
80
+ case point
81
+ when CGPointArray
82
+ x = self.x + point.x
83
+ y = self.y + point.y
84
+ CGPointArray[x, y]
85
+ when CGSizeArray
86
+ CGRectArray[self, point]
87
+ else
88
+ super
89
+ end
90
+ end
91
+
55
92
  end
56
93
 
57
94
  class CGSizeArray < Array
@@ -72,19 +109,91 @@ class CGSizeArray < Array
72
109
  self[1] = val
73
110
  end
74
111
 
112
+ # adds the sizes
113
+ def +(size)
114
+ case size
115
+ when CGSizeArray
116
+ width = self.width + size.width
117
+ height = self.height + size.height
118
+ CGSizeArray[width, height]
119
+ when CGPointArray
120
+ CGRectArray[size, self]
121
+ else
122
+ super
123
+ end
124
+ end
125
+
75
126
  end
76
127
 
77
128
 
129
+ class UIEdgeInsetsArray < Array
130
+
131
+ def top
132
+ return self[0]
133
+ end
134
+
135
+ def top= val
136
+ self[0] = val
137
+ end
138
+
139
+ def left
140
+ return self[1]
141
+ end
142
+
143
+ def left= val
144
+ self[1] = val
145
+ end
146
+
147
+ def bottom
148
+ return self[2]
149
+ end
150
+
151
+ def bottom= val
152
+ self[2] = val
153
+ end
154
+
155
+ def right
156
+ return self[3]
157
+ end
158
+
159
+ def right= val
160
+ self[3] = val
161
+ end
162
+
163
+ end
164
+
165
+
166
+ class UIOffsetArray < Array
167
+
168
+ def horizontal
169
+ return self[0]
170
+ end
171
+
172
+ def horizontal= val
173
+ self[0] = val
174
+ end
175
+
176
+ def vertical
177
+ return self[1]
178
+ end
179
+
180
+ def vertical= val
181
+ self[1] = val
182
+ end
183
+
184
+ end
185
+
78
186
  module SugarCube
79
187
  module CoreGraphics
80
188
  module_function
81
189
 
82
190
  def Size(w_or_size, h=nil)
83
191
  if not h
84
- if CGSize === w_or_size
192
+ case w_or_size
193
+ when CGSize
85
194
  w = w_or_size.width
86
195
  h = w_or_size.height
87
- elsif Array === w_or_size
196
+ when Array
88
197
  w = w_or_size[0]
89
198
  h = w_or_size[1]
90
199
  else
@@ -98,10 +207,11 @@ module SugarCube
98
207
 
99
208
  def Point(x_or_origin, y=nil)
100
209
  if not y
101
- if CGPoint === x_or_origin
210
+ case x_or_origin
211
+ when CGPoint
102
212
  x = x_or_origin.x
103
213
  y = x_or_origin.y
104
- elsif Array === x_or_origin
214
+ when Array
105
215
  x = x_or_origin[0]
106
216
  y = x_or_origin[1]
107
217
  else
@@ -113,31 +223,45 @@ module SugarCube
113
223
  return CGPointArray.new([x, y])
114
224
  end
115
225
 
116
- # Accepts 2 or 4 arguments. 2 arguments should be a point and a size,
226
+ # Accepts 1, 2 or 4 arguments.
227
+ # 1 argument should be an Array[4], CGRectArray, or CGRect
228
+ # 2 arguments should be a Point/CGPoint and a Size/CGSize,
117
229
  # 4 should be x, y, w, h
118
230
  def Rect(x_or_origin, y_or_size=nil, w=nil, h=nil)
119
231
  if not y_or_size
120
- if CGRectArray === x_or_origin
121
- x = x_or_origin[0][0]
122
- y = x_or_origin[0][1]
123
- w = x_or_origin[1][0]
124
- h = x_or_origin[1][1]
125
- elsif CGRect === x_or_origin
232
+ case x_or_origin
233
+ when CGRect
126
234
  x = x_or_origin.origin.x
127
235
  y = x_or_origin.origin.y
128
236
  w = x_or_origin.size.width
129
237
  h = x_or_origin.size.height
130
- elsif Array === x_or_origin
131
- x = x_or_origin[0]
132
- y = x_or_origin[1]
133
- w = x_or_origin[2]
134
- h = x_or_origin[3]
238
+ when CGRectArray
239
+ x = x_or_origin[0][0]
240
+ y = x_or_origin[0][1]
241
+ w = x_or_origin[1][0]
242
+ h = x_or_origin[1][1]
243
+ when Array
244
+ if x_or_origin.length == 2
245
+ x = x_or_origin[0][0]
246
+ y = x_or_origin[0][1]
247
+ w = x_or_origin[1][0]
248
+ h = x_or_origin[1][1]
249
+ elsif
250
+ x = x_or_origin[0]
251
+ y = x_or_origin[1]
252
+ w = x_or_origin[2]
253
+ h = x_or_origin[3]
254
+ else
255
+ raise RuntimeError.new("Invalid argument sent to Rect(#{x_or_origin.inspect})")
256
+ end
135
257
  else
136
258
  raise RuntimeError.new("Invalid argument sent to Rect(#{x_or_origin.inspect})")
137
259
  end
138
260
  elsif not w and not h
261
+ x_or_origin = Point(x_or_origin) unless x_or_origin.is_a? CGPointArray
139
262
  x = x_or_origin.x
140
263
  y = x_or_origin.y
264
+ y_or_size = Size(y_or_size) unless y_or_size.is_a? CGSizeArray
141
265
  w = y_or_size.width
142
266
  h = y_or_size.height
143
267
  else
@@ -147,5 +271,56 @@ module SugarCube
147
271
  return CGRectArray.new([[x, y], [w, h]])
148
272
  end
149
273
 
274
+ # Accepts 1 or 4 arguments.
275
+ # 1 argument should be an Array[4] or UIEdgeInset,
276
+ # 4 should be top, left, bottom, right
277
+ def EdgeInsets(top_or_inset, left=nil, bottom=nil, right=nil)
278
+ unless left or bottom or right
279
+ case top_or_inset
280
+ when UIEdgeInsets
281
+ top = top.top
282
+ left = top.left
283
+ bottom = top.bottom
284
+ right = top.right
285
+ when Array
286
+ top = top_or_inset[0]
287
+ left = top_or_inset[1]
288
+ bottom = top_or_inset[2]
289
+ right = top_or_inset[3]
290
+ when Numeric
291
+ top = left = bottom = right = top_or_inset
292
+ else
293
+ raise RuntimeError.new("Invalid argument sent to EdgeInsets(#{top_or_inset.inspect})")
294
+ end
295
+ else
296
+ top = top_or_inset
297
+ end
298
+ return UIEdgeInsetsArray.new([top, left, bottom, right])
299
+ end
300
+
301
+ # Accepts 1 or 2 arguments.
302
+ # 1 argument should be an Array[2] or UIOffset,
303
+ # 2 should be horizontal, vertical
304
+ def Offset(horizontal_or_offset, vertical=nil)
305
+ if not vertical
306
+ case horizontal_or_offset
307
+ when UIOffset
308
+ horizontal = horizontal_or_offset.horizontal
309
+ vertical = horizontal_or_offset.vertical
310
+ when Array
311
+ horizontal = horizontal_or_offset[0]
312
+ vertical = horizontal_or_offset[1]
313
+ when Fixnum
314
+ horizontal_or_offset =
315
+ vertical = horizontal_or_offset[0]
316
+ else
317
+ raise RuntimeError.new("Invalid argument sent to Offset(#{horizontal_or_offset.inspect})")
318
+ end
319
+ else
320
+ horizontal = horizontal_or_offset
321
+ end
322
+ return UIOffsetArray.new([horizontal, vertical])
323
+ end
324
+
150
325
  end
151
326
  end
@@ -1,5 +1,22 @@
1
1
  class Symbol
2
2
 
3
+ def get_default
4
+ to_s.get_default
5
+ end
6
+
7
+ def set_default val
8
+ to_s.set_default val
9
+ end
10
+
11
+ def remove_default
12
+ to_s.remove_default
13
+ end
14
+
15
+ end
16
+
17
+
18
+ class String
19
+
3
20
  def get_default
4
21
  NSUserDefaults.standardUserDefaults.objectForKey(self)
5
22
  end
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  class NSString
4
2
 
5
3
  def document
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  class Fixnum
4
2
 
5
3
  # 0xffffff.uicolor
@@ -17,8 +15,4 @@ class Fixnum
17
15
  UIColor.colorWithRed(red, green:green, blue:blue, alpha:alpha.to_f)
18
16
  end
19
17
 
20
- def percent
21
- self.to_f.percent
22
- end
23
-
24
18
  end
@@ -8,6 +8,10 @@ class NSString
8
8
  @uiimage = UIImage.imageNamed(self)
9
9
  end
10
10
 
11
+ def uiimageview
12
+ @uiimageview ||= (self.uiimage and self.uiimage.uiimageview or UIImageView.alloc.initWithImage(nil))
13
+ end
14
+
11
15
  def uifont(size=UIFont.systemFontSize)
12
16
  @uifont = UIFont.fontWithName(self, size:size)
13
17
  end
@@ -0,0 +1,26 @@
1
+ class Numeric
2
+
3
+ def percent
4
+ self / 100.0
5
+ end
6
+
7
+ def seconds
8
+ self
9
+ end
10
+ alias :second :seconds
11
+
12
+ def minutes
13
+ self * 60
14
+ end
15
+ alias :minute :minutes
16
+
17
+ def hours
18
+ self * 3600
19
+ end
20
+ alias :hour :hours
21
+
22
+ def later(user_info=nil, &fire)
23
+ NSTimer.scheduledTimerWithTimeInterval(self.to_f, target: fire, selector: 'call:', userInfo: user_info, repeats: false)
24
+ end
25
+
26
+ end
@@ -34,6 +34,12 @@ class Symbol
34
34
  attr_accessor :system_fonts
35
35
  attr_accessor :font_sizes
36
36
  attr_accessor :returnkeys
37
+ attr_accessor :activityindicator_styles
38
+ attr_accessor :tableview_styles
39
+ attr_accessor :statusbar_styles
40
+ attr_accessor :barmetrics
41
+ attr_accessor :barbuttomitems
42
+ attr_accessor :keyboardtypes
37
43
  end
38
44
 
39
45
  @devices = {
@@ -137,10 +143,71 @@ class Symbol
137
143
  emergencycall: UIReturnKeyEmergencyCall,
138
144
  }
139
145
 
146
+ @activityindicator_styles = {
147
+ large: UIActivityIndicatorViewStyleWhiteLarge,
148
+ whitelarge: UIActivityIndicatorViewStyleWhiteLarge,
149
+ white: UIActivityIndicatorViewStyleWhite,
150
+ gray: UIActivityIndicatorViewStyleGray,
151
+ }
152
+
153
+ @tableview_styles = {
154
+ plain: UITableViewStylePlain,
155
+ grouped: UITableViewStyleGrouped,
156
+ }
157
+
158
+ @statusbar_styles = {
159
+ default: UIStatusBarStyleDefault,
160
+ black: UIStatusBarStyleBlackOpaque,
161
+ translucent: UIStatusBarStyleBlackTranslucent,
162
+ }
163
+
164
+ @barmetrics = {
165
+ default: UIBarMetricsDefault,
166
+ landscape: UIBarMetricsLandscapePhone,
167
+ }
168
+
169
+ @barbuttomitems = {
170
+ done: UIBarButtonSystemItemDone,
171
+ cancel: UIBarButtonSystemItemCancel,
172
+ edit: UIBarButtonSystemItemEdit,
173
+ save: UIBarButtonSystemItemSave,
174
+ add: UIBarButtonSystemItemAdd,
175
+ flexiblespace: UIBarButtonSystemItemFlexibleSpace,
176
+ fixedspace: UIBarButtonSystemItemFixedSpace,
177
+ compose: UIBarButtonSystemItemCompose,
178
+ reply: UIBarButtonSystemItemReply,
179
+ action: UIBarButtonSystemItemAction,
180
+ organize: UIBarButtonSystemItemOrganize,
181
+ bookmarks: UIBarButtonSystemItemBookmarks,
182
+ search: UIBarButtonSystemItemSearch,
183
+ refresh: UIBarButtonSystemItemRefresh,
184
+ stop: UIBarButtonSystemItemStop,
185
+ camera: UIBarButtonSystemItemCamera,
186
+ trash: UIBarButtonSystemItemTrash,
187
+ play: UIBarButtonSystemItemPlay,
188
+ pause: UIBarButtonSystemItemPause,
189
+ rewind: UIBarButtonSystemItemRewind,
190
+ fastforward: UIBarButtonSystemItemFastForward,
191
+ undo: UIBarButtonSystemItemUndo,
192
+ redo: UIBarButtonSystemItemRedo,
193
+ pagecurl: UIBarButtonSystemItemPageCurl,
194
+ }
195
+
196
+ @keyboardtypes = {
197
+ default: UIKeyboardTypeDefault,
198
+ asciicapable: UIKeyboardTypeASCIICapable,
199
+ numbersandpunctuation: UIKeyboardTypeNumbersAndPunctuation,
200
+ url: UIKeyboardTypeURL,
201
+ numberpad: UIKeyboardTypeNumberPad,
202
+ phonepad: UIKeyboardTypePhonePad,
203
+ namephonepad: UIKeyboardTypeNamePhonePad,
204
+ emailaddress: UIKeyboardTypeEmailAddress,
205
+ }
206
+
140
207
  private
141
208
  def look_in(here)
142
209
  return here[self] if here.has_key? self
143
- raise SugarNotFoundException.new(self)
210
+ raise SugarNotFoundException.new(self.inspect)
144
211
  end
145
212
 
146
213
  public
@@ -171,6 +238,7 @@ class Symbol
171
238
  def uicontrolstate
172
239
  look_in(Symbol.controlstates)
173
240
  end
241
+ alias :uistate :uicontrolstate
174
242
 
175
243
  def uicontrolevent
176
244
  look_in(Symbol.controlevents)
@@ -180,18 +248,45 @@ class Symbol
180
248
  look_in(Symbol.returnkeys)
181
249
  end
182
250
 
251
+ def uiactivityindicatorstyle
252
+ look_in(Symbol.activityindicator_styles)
253
+ end
254
+ alias :uiactivityindicatorviewstyle :uiactivityindicatorstyle
255
+
256
+ def uitablestyle
257
+ look_in(Symbol.tableview_styles)
258
+ end
259
+ alias :uitableviewstyle :uitablestyle
260
+
261
+ def uistatusbarstyle
262
+ look_in(Symbol.statusbar_styles)
263
+ end
264
+
265
+ def uibarmetrics
266
+ look_in(Symbol.barmetrics)
267
+ end
268
+
269
+ def uibarbuttonitem
270
+ look_in(Symbol.barbuttomitems)
271
+ end
272
+
273
+ def uikeyboardtype
274
+ look_in(Symbol.keyboardtypes)
275
+ end
276
+
183
277
  def uifont(size=UIFont.systemFontSize)
184
278
  # system fonts
185
- begin
279
+ if Symbol.system_fonts.has_key? self
186
280
  font = look_in(Symbol.system_fonts)
187
281
  if Symbol === size
188
282
  size = Symbol.font_sizes.fetch(size).uifontsize
189
283
  end
190
- return UIFont.send(font, size)
191
- rescue SugarNotFoundException
284
+ font = UIFont.send(font, size)
285
+ else
192
286
  size = look_in(font_sizes).uifontsize
193
- return UIFont.systemFontOfSize(size)
287
+ font = UIFont.systemFontOfSize(size)
194
288
  end
289
+ font
195
290
  end
196
291
 
197
292
  def uifontsize
@@ -202,8 +297,4 @@ class Symbol
202
297
  return size.to_f
203
298
  end
204
299
 
205
- def to_teacup_stylesheet
206
- Teacup::Stylesheet[self]
207
- end
208
-
209
300
  end
@@ -5,14 +5,14 @@ class Symbol
5
5
  end
6
6
 
7
7
  def uicolor(alpha=nil)
8
- begin
8
+ if Symbol.uicolors.has_key? self
9
9
  # iOS colors
10
10
  color = UIColor.send(look_in(Symbol.uicolors))
11
11
 
12
12
  if not alpha.nil?
13
13
  color = color.colorWithAlphaComponent(alpha.to_f)
14
14
  end
15
- rescue SugarNotFoundException
15
+ else
16
16
  # css colors
17
17
  color = look_in(Symbol.css_colors).uicolor(alpha)
18
18
  end
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  class UIControl
4
2
 
5
3
  # event blocks need to be retained, and the addTarget method explicitly does
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  class UIImage
4
2
  def uiimage ; self ; end
5
3
 
@@ -13,4 +11,9 @@ class UIImage
13
11
  color
14
12
  end
15
13
 
14
+ def uiimageview
15
+ @uiimageview = UIImageView.alloc.initWithImage(self)
16
+ end
17
+
18
+
16
19
  end
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  class UIView
4
2
 
5
3
  def <<(view)
@@ -12,7 +10,39 @@ class UIView
12
10
  "#{self.superview ? ' child of ' + self.superview.class.name : ''}"
13
11
  end
14
12
 
15
- def fade_out(duration=0.3, options={}, &after)
13
+ def show
14
+ self.hidden = false
15
+ self
16
+ end
17
+
18
+ def hide
19
+ self.hidden = true
20
+ self
21
+ end
22
+
23
+ def _after_proc(after)
24
+ after ? proc { |finished|
25
+ if after.arity == 0
26
+ after.call
27
+ else
28
+ after.call(finished)
29
+ end
30
+ } : nil
31
+ end
32
+
33
+ # If options is a Numeric, it is used as the duration. Otherwise, duration
34
+ # is an option, and defaults to 0.3. All the transition methods work this
35
+ # way.
36
+ def fade_out(options={}, &after)
37
+ if options.is_a? Numeric
38
+ duration = options
39
+ options = {}
40
+ else
41
+ duration = options[:duration] || 0.3
42
+ end
43
+
44
+ after = _after_proc(after)
45
+
16
46
  UIView.animateWithDuration(duration,
17
47
  delay: options[:delay] || 0,
18
48
  options: options[:options] || UIViewAnimationOptionCurveLinear,
@@ -20,35 +50,92 @@ class UIView
20
50
  self.layer.opacity = options[:opacity] || 0
21
51
  }, completion:after
22
52
  )
53
+ self
23
54
  end
24
55
 
25
- def move_to(position, duration=0.3, options={}, &after)
56
+ def fade_in(options={}, &after)
57
+ if options.is_a? Numeric
58
+ duration = options
59
+ options = {}
60
+ else
61
+ duration = options[:duration] || 0.3
62
+ end
63
+
64
+ options[:opacity] = 1.0
65
+ fade_out(options, &after)
66
+ end
67
+
68
+ def move_to(position, options={}, &after)
69
+ if options.is_a? Numeric
70
+ duration = options
71
+ options = {}
72
+ else
73
+ duration = options[:duration] || 0.3
74
+ end
75
+
76
+ after = _after_proc(after)
77
+
26
78
  UIView.animateWithDuration(duration,
27
79
  delay: options[:delay] || 0,
28
80
  options: options[:options] || UIViewAnimationOptionCurveLinear,
29
81
  animations: proc{
30
82
  f = self.frame
31
- f.origin = position
32
- self.frame = position
83
+ f.origin = SugarCube::CoreGraphics::Point(position)
84
+ self.frame = f
33
85
  }, completion:after
34
86
  )
87
+ self
35
88
  end
36
89
 
37
- def delta_to(delta, duration=0.3, options={}, &after)
90
+ def delta_to(delta, options={}, &after)
38
91
  f = self.frame
39
- delta = Point(delta)
40
- position = Point(f.origin)
41
- position.x += delta.x
42
- position.y += delta.y
43
- UIView.animateWithDuration(duration,
44
- delay: options[:delay] || 0,
45
- options: options[:options] || UIViewAnimationOptionCurveLinear,
46
- animations: proc{
47
- f = self.frame
48
- f.origin = position
49
- self.frame = position
50
- }, completion:after
51
- )
92
+ delta = SugarCube::CoreGraphics::Point(delta)
93
+ position = SugarCube::CoreGraphics::Point(f.origin)
94
+ move_to(position + delta, options, &after)
95
+ self
96
+ end
97
+
98
+ def slide(direction, options={}, &after)
99
+ size = UIScreen.mainScreen.bounds.size
100
+ case direction
101
+ when :left
102
+ delta_to([-size.width, 0], options, &after)
103
+ when :right
104
+ delta_to([+size.width, 0], options, &after)
105
+ when :up
106
+ delta_to([0, -size.height], options, &after)
107
+ when :down
108
+ delta_to([0, +size.height], options, &after)
109
+ else
110
+ raise "Unknown direction #{direction.inspect}"
111
+ end
112
+ self
113
+ end
114
+
115
+ def shake(options={})
116
+ if options.is_a? Numeric
117
+ duration = options
118
+ options = {}
119
+ else
120
+ duration = options[:duration] || 0.3
121
+ end
122
+
123
+ offset = options[:offset] || 8
124
+ repeat = options[:repeat] || 3
125
+ duration /= repeat
126
+ keypath = options[:keypath] || 'transform.translation.x'
127
+
128
+ origin = 0
129
+ left = -offset
130
+ right = +offset
131
+
132
+ animation = CAKeyframeAnimation.animationWithKeyPath(keypath)
133
+ animation.duration = duration
134
+ animation.repeatCount = repeat
135
+ animation.values = [origin, left, right, origin]
136
+ animation.keyTimes = [0, 0.25, 0.75, 1.0]
137
+ self.layer.addAnimation(animation, forKey:'shake')
138
+ self
52
139
  end
53
140
 
54
141
  end
@@ -0,0 +1,27 @@
1
+ class UINavigationController
2
+
3
+ def push(view_controller)
4
+ self.pushViewController(view_controller, animated: true)
5
+ self
6
+ end
7
+
8
+ def <<(view_controller)
9
+ self.pushViewController(view_controller, animated: true)
10
+ self
11
+ end
12
+
13
+ def pop(to_view=nil)
14
+ return self.!(to_view) if to_view
15
+
16
+ self.popViewControllerAnimated(true)
17
+ end
18
+
19
+ def !(to_view=nil)
20
+ if to_view
21
+ self.popToViewController(to_view, animated: true)
22
+ else
23
+ self.popToRootViewControllerAnimated(true)
24
+ end
25
+ end
26
+
27
+ end
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.2.3'
2
+ Version = '0.2.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-14 00:00:00.000000000 Z
13
+ date: 2012-07-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &70314746913920 !ruby/object:Gem::Requirement
17
+ requirement: &70269784397300 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70314746913920
25
+ version_requirements: *70269784397300
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70314746929400 !ruby/object:Gem::Requirement
28
+ requirement: &70269784412820 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70314746929400
36
+ version_requirements: *70269784412820
37
37
  description: ! 'CocoaTouch/iOS is a *verbose* framework. These extensions hope to
38
38
  make
39
39
 
@@ -70,10 +70,10 @@ files:
70
70
  - lib/sugarcube/document.rb
71
71
  - lib/sugarcube/exceptions.rb
72
72
  - lib/sugarcube/fixnum.rb
73
- - lib/sugarcube/float.rb
74
73
  - lib/sugarcube/notifications.rb
75
74
  - lib/sugarcube/nsstring.rb
76
75
  - lib/sugarcube/nsurl.rb
76
+ - lib/sugarcube/numeric.rb
77
77
  - lib/sugarcube/symbol.rb
78
78
  - lib/sugarcube/symbol/symbol_uicolor.rb
79
79
  - lib/sugarcube/uicolor.rb
@@ -81,6 +81,7 @@ files:
81
81
  - lib/sugarcube/uifont.rb
82
82
  - lib/sugarcube/uiimage.rb
83
83
  - lib/sugarcube/uiview.rb
84
+ - lib/sugarcube/uiviewcontroller.rb
84
85
  - lib/sugarcube/version.rb
85
86
  - sugarcube.gemspec
86
87
  homepage: https://github.com/fusionbox/sugarcube
@@ -1,9 +0,0 @@
1
-
2
-
3
- class Float
4
-
5
- def percent
6
- self / 100.0
7
- end
8
-
9
- end