sugarcube 0.12 → 0.13

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
@@ -416,6 +416,9 @@ image.rotate(:right)
416
416
  image.rotate(:flip) # 180° - if you have a better name, let me know!
417
417
  image.rotate(45.degrees)
418
418
 
419
+ image.scale_to(new_size) # won't stretch
420
+ image.in_rect(frame) # stretches
421
+
419
422
  # default insets are UIEdgeInsetsZero
420
423
  image.tileable
421
424
  image.tileable(insets)
@@ -423,7 +426,7 @@ image.stretchable
423
426
  image.stretchable(insets)
424
427
  ```
425
428
 
426
- UIAlertView
429
+ UIAlertView
427
430
  --------
428
431
 
429
432
  Accepts multiple buttons and success and cancel handlers. In its simplest
@@ -447,6 +450,36 @@ UIAlertView.alert "I mean, is this cool?", buttons: %w[No! Sure! Hmmmm],
447
450
  success: proc { |pressed| self.proceed if pressed == "Sure!" }
448
451
  ```
449
452
 
453
+ UIActionSheet
454
+ --------
455
+
456
+ This is very similar to `UIAlertView.alert`, but instead of `cancel` and
457
+ `success` handlers, you can have `cancel, success, and destructive` handlers,
458
+ and there is no `message` argument.
459
+
460
+ If you use an array of buttons (which you probably *should*), the order of
461
+ arguments is `[:cancel, :destructive, :others, ...]`. If you *dont* want a
462
+ cancel or destructive button, pass `nil` in place.
463
+
464
+ ```ruby
465
+ # simple
466
+ UIActionSheet.alert 'This is happening, OK?' { self.happened! }
467
+ # a little more complex, with cancel and destructive buttons
468
+ UIActionSheet.alert('This is happening, OK?', buttons: ['Sure!', 'OK']
469
+ ) {
470
+ self.happened!
471
+ }
472
+
473
+ UIActionSheet.alert('Should I?', buttons: [nil, nil, 'OK', 'Nevermind']) { |pressed|
474
+ self.do_it if pressed == 'OK'
475
+ }
476
+
477
+ UIActionSheet.alert 'I mean, is this cool?', buttons: ['Nah', 'With fire!', 'Sure', 'whatever'],
478
+ cancel: proc { self.cancel },
479
+ destructive: proc { self.kill_it_with_fire }
480
+ success: proc { |pressed| self.proceed if pressed == 'Sure' }
481
+ ```
482
+
450
483
  UIView
451
484
  --------
452
485
 
@@ -773,11 +806,12 @@ test[:my] = 'new'
773
806
 
774
807
  Instead, just use the coercion methods `Rect()`, `Size()` and `Point()`. They
775
808
  will happily convert most sensible (and some non-sensible) arguments into a
776
- `CGRect/CGSize/CGPoint` struct. For more CoreGraphics additions, you should use
777
- [geomotion][] by [Clay Allsopp][]. It adds methods to `CGRect`, `CGPoint`, and
778
- `CGSize` to make these structures more rubyesque (these methods used to be part
779
- of SugarCube, but were removed in an attempt to decrease the amount of
780
- duplicated code).
809
+ `CGRect/CGSize/CGPoint` struct.
810
+
811
+ For more CoreGraphics additions, you should use [geomotion][] by [Clay
812
+ Allsopp][]. It adds methods to `CGRect`, `CGPoint`, and `CGSize` to make these
813
+ structures more rubyesque (these methods used to be part of SugarCube, but were
814
+ removed in an attempt to decrease the amount of duplicated code).
781
815
 
782
816
  [geomotion]: https://github.com/clayallsopp
783
817
  [Clay Allsopp]: https://github.com/clayallsopp/geomotion
@@ -1020,6 +1054,16 @@ You can analyze `UIViewController` hierarchies, too. There's even a handy
1020
1054
  => #<MainScreenController:0xac23b80>
1021
1055
  ```
1022
1056
 
1057
+ ##### Nothing is sacred
1058
+
1059
+ The adjust and tree methods act on global objects. Once either of these methods
1060
+ is used, you can access that global if you want:
1061
+
1062
+ ```ruby
1063
+ $sugarcube_view # => the view (or any object) being 'adjusted' (accessible using `adjust` or `a`)
1064
+ $sugarcube_items # => the list of views that was output using `tree`
1065
+ ```
1066
+
1023
1067
 
1024
1068
  Pointers
1025
1069
  ----------
@@ -1,6 +1,6 @@
1
1
  class UIActionSheet
2
2
 
3
- # UIActionSheet.alert("message",
3
+ # UIActionSheet.alert("title",
4
4
  # # The first button is considered the 'cancel' button, for the purposes of
5
5
  # # whether the cancel or success handler gets called, the second button is
6
6
  # # the 'destructive' button, and the rest are plain old buttons.
@@ -10,49 +10,78 @@ class UIActionSheet
10
10
  # success: proc{ |pressed| puts "pressed: #{pressed}" },
11
11
  # )
12
12
  def self.alert(title, options={}, &block)
13
- if options.is_a? String
14
- options = {message: options}
15
- end
16
-
17
13
  # create the delegate
18
14
  delegate = SugarCube::ActionSheetDelegate.new
19
15
  delegate.on_success = options[:success] || block
20
- delegate.on_destructive = options[:destructive] || block
16
+ delegate.on_destructive = options[:destructive]
21
17
  delegate.on_cancel = options[:cancel]
22
18
  delegate.send(:retain)
23
19
 
24
20
  args = [title] # initWithTitle:
25
- args << options[:message] # message:
26
21
  args << delegate # delegate:
27
22
 
28
- buttons = options[:buttons] || []
23
+ buttons = []
24
+ buttons.concat(options[:buttons]) if options[:buttons]
25
+
29
26
  if buttons.empty?
30
- # cancelButtonTitle: is first, so check for cancel
31
- buttons << "Cancel" if options[:cancel]
32
- # destructiveButtonTitle: is first, so check for cancel
33
- buttons << "Cancel" if options[:cancel]
34
- # otherButtonTitles:
35
- buttons << "OK" if options[:success] or buttons.empty?
36
- elsif buttons.length == 1 and options[:cancel]
37
- raise "If you only have one button, use a :success handler, not :cancel (and definitely not BOTH)"
38
- end
27
+ # cancelButtonTitle: is first, so check for cancel handler
28
+ if options[:cancel]
29
+ buttons << 'Cancel'
30
+ else
31
+ buttons << nil
32
+ end
39
33
 
40
- # the button titles. These are passed to the success handler.
41
- delegate.buttons = buttons
34
+ # destructiveButtonTitle, check for destructive handler
35
+ if options[:destructive]
36
+ buttons << 'Delete'
37
+ else
38
+ buttons << nil
39
+ end
40
+ elsif buttons.length == 1 and (options[:cancel] or options[:destructive])
41
+ raise 'If you only have one button, use a :success handler, not :cancel or :destructive'
42
+ end
42
43
 
43
44
  # uses localized buttons in the actual alert
44
- args.concat(buttons.map{ |s| s.localized })
45
+ if buttons.length == 0
46
+ buttons = [nil, nil]
47
+ elsif buttons.length == 1
48
+ buttons << nil
49
+ end
50
+
51
+ last_index = buttons.length - 1
52
+ offset = 0
53
+
54
+ button_index_map = {}
55
+ if buttons[1] # destructive
56
+ button_index_map[0] = buttons[1]
57
+ offset += 1
58
+ else
59
+ last_index -= 1
60
+ end
61
+
62
+ if buttons[0] # cancel
63
+ button_index_map[last_index] = buttons[0]
64
+ end
65
+
66
+ buttons[2..-1].each_with_index { |button, index|
67
+ button_index_map[index + offset] = button
68
+ }
69
+ # the button titles, mapped to how UIActionSheet orders them. These are passed to the success handler.
70
+ delegate.button_index_map = button_index_map
71
+
72
+ args.concat(buttons.map{ |s| s ? s.localized : nil })
45
73
  args << nil # otherButtonTitles:..., nil
46
74
 
47
75
  alert = self.alloc
48
- alert.send('initWithTitle:message:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:', *args)
49
- alert.show
76
+ alert.send('initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:', *args)
77
+ window = UIApplication.sharedApplication.keyWindow || UIApplication.sharedApplication.windows[0]
78
+ alert.showInView(window)
50
79
  alert
51
80
  end
52
81
 
53
82
  private
54
83
  def dummy
55
- self.initWithTitle(nil, message:nil, delegate:nil, cancelButtonTitle:nil, destructiveButtonTitle:nil, otherButtonTitles:nil)
84
+ self.initWithTitle(nil, delegate:nil, cancelButtonTitle:nil, destructiveButtonTitle:nil, otherButtonTitles:nil)
56
85
  end
57
86
 
58
87
  end
@@ -60,12 +89,12 @@ end
60
89
 
61
90
  module SugarCube
62
91
  class ActionSheetDelegate
63
- attr_accessor :buttons
92
+ attr_accessor :button_index_map
64
93
  attr_accessor :on_cancel
65
94
  attr_accessor :on_destructive
66
95
  attr_accessor :on_success
67
96
 
68
- def alertSheet(alert, didDismissWithButtonIndex:index)
97
+ def actionSheet(alert, didDismissWithButtonIndex:index)
69
98
  if index == alert.destructiveButtonIndex && on_destructive
70
99
  on_destructive.call
71
100
  elsif index == alert.cancelButtonIndex && on_cancel
@@ -74,7 +103,7 @@ module SugarCube
74
103
  if on_success.arity == 0
75
104
  on_success.call
76
105
  else
77
- button = buttons[index]
106
+ button = button_index_map[index]
78
107
  on_success.call(button)
79
108
  end
80
109
  end
@@ -6,6 +6,10 @@ class UIColor
6
6
  _sugarcube_colors[:red]
7
7
  end
8
8
 
9
+ def cgcolor
10
+ self.CGColor
11
+ end
12
+
9
13
  def green
10
14
  _sugarcube_colors[:green]
11
15
  end
@@ -35,8 +35,15 @@ class UIView
35
35
  end
36
36
  end
37
37
 
38
+ # superview << view
39
+ # => superview.addSubview(view)
38
40
  def <<(view)
39
- self.addSubview view
41
+ self.addSubview(view)
42
+ return self
43
+ end
44
+
45
+ def unshift(view)
46
+ self.insertSubview(view, atIndex:0)
40
47
  return self
41
48
  end
42
49
 
@@ -139,6 +146,21 @@ class UIView
139
146
  fade(options, &after)
140
147
  end
141
148
 
149
+ # Changes the layer opacity to 0 and then removes the view from its superview
150
+ # @see #fade_out
151
+ def fade_out_and_remove(options={}, &after)
152
+ if options.is_a? Numeric
153
+ options = { duration: options }
154
+ end
155
+
156
+ after_remove = proc {
157
+ removeFromSuperview
158
+ after.call if after
159
+ }
160
+
161
+ fade_out(options, &after_remove)
162
+ end
163
+
142
164
  def move_to(position, options={}, &after)
143
165
  if options.is_a? Numeric
144
166
  options = { duration: options }
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.12'
2
+ Version = '0.13'
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.12'
4
+ version: '0.13'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: