Class: UIView
- Defined in:
- lib/sugarcube-gestures/gestures.rb,
lib/sugarcube/uiview.rb,
lib/sugarcube/to_s/uiview.rb
Overview
BubbleWrap has these same methods, but the logic and options are a little different. In the spirit of open source, I am blatantly copying their code, changing it to suit my needs, and offering it here
Class Method Summary collapse
-
.animate(options = {}, &animations) ⇒ Object
If options is a Numeric, it is used as the duration.
-
.first_responder ⇒ Object
returns the first responder, starting at the Window and searching every subview.
Instance Method Summary collapse
-
#<<(view) ⇒ Object
superview << view => superview.addSubview(view).
-
#animate(options = {}, &animations) ⇒ Object
Same as UIView##animate, but acts on self.
-
#controller ⇒ Object
returns the nearest nextResponder instance that is a UIViewController.
- #delta_to(delta, options = {}, &after) ⇒ Object
-
#fade(options = {}, &after) ⇒ Object
Changes the layer opacity.
-
#fade_in(options = {}, &after) ⇒ Object
Changes the layer opacity to 1.
-
#fade_out(options = {}, &after) ⇒ Object
Changes the layer opacity to 0.
-
#fade_out_and_remove(options = {}, &after) ⇒ Object
Changes the layer opacity to 0 and then removes the view from its superview.
-
#first_responder ⇒ Object
returns the first responder, or nil if it cannot be found.
- #hide ⇒ Object
- #move_to(position, options = {}, &after) ⇒ Object
-
#on_gesture(klass, options = {}) {|recognizer| ... } ⇒ Object
A generic gesture adder, but accepts a block like the other gesture methods.
- #on_pan(fingers_or_options = nil) {|recognizer| ... } ⇒ Object
- #on_pinch {|recognizer| ... } ⇒ Object
- #on_press(duration_or_options = nil) {|recognizer| ... } ⇒ Object
- #on_rotate {|recognizer| ... } ⇒ Object
- #on_swipe(direction_or_options = nil) {|recognizer| ... } ⇒ Object
- #on_tap(taps_or_options = nil) {|recognizer| ... } ⇒ Object
- #shake(options = {}) ⇒ Object
- #show ⇒ Object
- #slide(direction, options = {}, &after) ⇒ Object
- #to_s(options = {}) ⇒ Object
- #unshift(view) ⇒ Object
Class Method Details
.animate(options = {}, &animations) ⇒ Object
If options is a Numeric, it is used as the duration. Otherwise, duration is an option, and defaults to 0.3. All the transition methods work this way.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sugarcube/uiview.rb', line 63 def self.animate(={}, &animations) if .is_a? Numeric duration = = {} else duration = [:duration] || 0.3 end after_animations = [:after] if after_animations if after_animations.arity == 0 after_adjusted = proc { |finished| after_animations.call } else after_adjusted = proc { |finished| after_animations.call(finished) } end else after_adjusted = nil end UIView.animateWithDuration( duration, delay: [:delay] || 0, options: [:options] || UIViewAnimationOptionCurveEaseInOut, animations: proc, completion: after_adjusted ) nil end |
.first_responder ⇒ Object
returns the first responder, starting at the Window and searching every subview
5 6 7 |
# File 'lib/sugarcube/uiview.rb', line 5 def first_responder UIApplication.sharedApplication.keyWindow.first_responder end |
Instance Method Details
#<<(view) ⇒ Object
superview << view => superview.addSubview(view)
40 41 42 43 |
# File 'lib/sugarcube/uiview.rb', line 40 def <<(view) self.addSubview(view) return self end |
#animate(options = {}, &animations) ⇒ Object
Same as UIView##animate, but acts on self
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sugarcube/uiview.rb', line 92 def animate(={}, &animations) if .is_a? Numeric duration = = {} else duration = [:duration] || 0.3 end assign = [:assign] || {} UIView.animate() { animations.call if animations assign.each_pair do |key, value| self.send("#{key}=", value) end } self end |
#controller ⇒ Object
returns the nearest nextResponder instance that is a UIViewController. Goes up the responder chain until the nextResponder is a UIViewController subclass, or returns nil if none is found.
28 29 30 31 32 33 34 35 36 |
# File 'lib/sugarcube/uiview.rb', line 28 def controller if nextResponder && nextResponder.is_a?(UIViewController) nextResponder elsif nextResponder nextResponder.controller else nil end end |
#delta_to(delta, options = {}, &after) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/sugarcube/uiview.rb', line 181 def delta_to(delta, ={}, &after) f = self.frame delta = SugarCube::CoreGraphics::Point(delta) position = SugarCube::CoreGraphics::Point(f.origin) move_to(position + delta, , &after) self end |
#fade(options = {}, &after) ⇒ Object
Changes the layer opacity.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sugarcube/uiview.rb', line 113 def fade(={}, &after) if .is_a? Numeric = { opacity: } end [:after] ||= after animate() { self.layer.opacity = [:opacity] } end |
#fade_in(options = {}, &after) ⇒ Object
Changes the layer opacity to 1.
139 140 141 142 143 144 145 146 147 |
# File 'lib/sugarcube/uiview.rb', line 139 def fade_in(={}, &after) if .is_a? Numeric = { duration: } end [:opacity] ||= 1.0 fade(, &after) end |
#fade_out(options = {}, &after) ⇒ Object
Changes the layer opacity to 0.
127 128 129 130 131 132 133 134 135 |
# File 'lib/sugarcube/uiview.rb', line 127 def fade_out(={}, &after) if .is_a? Numeric = { duration: } end [:opacity] ||= 0.0 fade(, &after) end |
#fade_out_and_remove(options = {}, &after) ⇒ Object
Changes the layer opacity to 0 and then removes the view from its superview
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/sugarcube/uiview.rb', line 151 def fade_out_and_remove(={}, &after) if .is_a? Numeric = { duration: } end original_opacity = self.layer.opacity after_remove = proc { removeFromSuperview self.layer.opacity = original_opacity after.call if after } fade_out(, &after_remove) end |
#first_responder ⇒ Object
returns the first responder, or nil if it cannot be found
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sugarcube/uiview.rb', line 11 def first_responder if self.firstResponder? return self end found = nil self.subviews.each do |subview| found = subview.first_responder break if found end return found end |
#hide ⇒ Object
55 56 57 58 |
# File 'lib/sugarcube/uiview.rb', line 55 def hide self.hidden = true self end |
#move_to(position, options = {}, &after) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/sugarcube/uiview.rb', line 167 def move_to(position, ={}, &after) if .is_a? Numeric = { duration: } end [:after] ||= after animate() { f = self.frame f.origin = SugarCube::CoreGraphics::Point(position) self.frame = f } end |
#on_gesture(recognizer) ⇒ Object #on_gesture(recognizer_class) ⇒ Object
A generic gesture adder, but accepts a block like the other gesture methods
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sugarcube-gestures/gestures.rb', line 19 def on_gesture(klass, ={}, &proc) if klass.is_a? UIGestureRecognizer recognizer = klass recognizer.addTarget(self, action:'sugarcube_handle_gesture:') else recognizer = klass.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:') end .each do |method, value| recognizer.send(method, value) end sugarcube_add_gesture(proc, recognizer) end |
#on_tap(taps) ⇒ Object #on_tap(options) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sugarcube-gestures/gestures.rb', line 113 def on_pan(=nil, &proc) fingers = nil min_fingers = nil max_fingers = nil if if .is_a? Hash fingers = [:fingers] || fingers min_fingers = [:min_fingers] || min_fingers max_fingers = [:max_fingers] || max_fingers else fingers = end end # if fingers is assigned, but not min/max, assign it as a default min_fingers ||= fingers max_fingers ||= fingers recognizer = UIPanGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:') recognizer.maximumNumberOfTouches = min_fingers if min_fingers recognizer.minimumNumberOfTouches = max_fingers if max_fingers sugarcube_add_gesture(proc, recognizer) end |
#on_pinch {|recognizer| ... } ⇒ Object
59 60 61 62 |
# File 'lib/sugarcube-gestures/gestures.rb', line 59 def on_pinch(&proc) recognizer = UIPinchGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:') sugarcube_add_gesture(proc, recognizer) end |
#on_press(duration) ⇒ Object #on_tap(options) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/sugarcube-gestures/gestures.rb', line 145 def on_press(=nil, &proc) duration = nil taps = nil fingers = nil if if .is_a? Hash duration = [:duration] || duration taps = [:taps] || taps fingers = [:fingers] || fingers else duration = end end recognizer = UILongPressGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:') recognizer.minimumPressDuration = duration if duration recognizer.numberOfTapsRequired = taps if taps recognizer.numberOfTouchesRequired = fingers if fingers sugarcube_add_gesture(proc, recognizer) end |
#on_rotate {|recognizer| ... } ⇒ Object
65 66 67 68 |
# File 'lib/sugarcube-gestures/gestures.rb', line 65 def on_rotate(&proc) recognizer = UIRotationGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:') sugarcube_add_gesture(proc, recognizer) end |
#on_swipe(taps) ⇒ Object #on_swipe(options) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sugarcube-gestures/gestures.rb', line 76 def on_swipe(=nil, &proc) direction = UISwipeGestureRecognizerDirectionRight fingers = nil if if .is_a? Hash direction = [:direction] || direction fingers = [:fingers] || fingers else direction = end end case direction when :left direction = UISwipeGestureRecognizerDirectionLeft when :right direction = UISwipeGestureRecognizerDirectionRight when :up direction = UISwipeGestureRecognizerDirectionUp when :down direction = UISwipeGestureRecognizerDirectionDown end recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:') recognizer.direction = direction if direction recognizer.numberOfTouchesRequired = fingers if fingers sugarcube_add_gesture(proc, recognizer) end |
#on_tap(taps) ⇒ Object #on_tap(options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sugarcube-gestures/gestures.rb', line 39 def on_tap(=nil, &proc) taps = nil fingers = nil if if .is_a? Hash taps = [:taps] || taps fingers = [:fingers] || fingers else taps = end end recognizer = UITapGestureRecognizer.alloc.initWithTarget(self, action:'sugarcube_handle_gesture:') recognizer.numberOfTapsRequired = taps if taps recognizer.numberOfTouchesRequired = fingers if fingers sugarcube_add_gesture(proc, recognizer) end |
#shake(options = {}) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/sugarcube/uiview.rb', line 213 def shake(={}) if .is_a? Numeric duration = = {} else duration = [:duration] || 0.3 end offset = [:offset] || 8 repeat = [:repeat] || 3 if repeat == Float::INFINITY duration = 0.1 else duration /= repeat end keypath = [:keypath] || 'transform.translation.x' origin = [:origin] || 0 left = origin - offset right = origin + offset animation = CAKeyframeAnimation.animationWithKeyPath(keypath) animation.duration = duration animation.repeatCount = repeat animation.values = [origin, left, right, origin] animation.keyTimes = [0, 0.25, 0.75, 1.0] self.layer.addAnimation(animation, forKey:'shake') self end |
#show ⇒ Object
50 51 52 53 |
# File 'lib/sugarcube/uiview.rb', line 50 def show self.hidden = false self end |
#slide(direction, options = {}, &after) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sugarcube/uiview.rb', line 189 def (direction, ={}, &after) if .is_a? Numeric = {size: } end case direction when :left size = [:size] || self.bounds.size.width delta_to([-size, 0], , &after) when :right size = [:size] || self.bounds.size.width delta_to([+size, 0], , &after) when :up size = [:size] || self.bounds.size.height delta_to([0, -size], , &after) when :down size = [:size] || self.bounds.size.height delta_to([0, +size], , &after) else raise "Unknown direction #{direction.inspect}" end self end |
#to_s(options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sugarcube/to_s/uiview.rb', line 3 def to_s(={}) [:superview] = true if [:superview].nil? if self.respond_to? :stylename and self.stylename suffix = ' stylename: ' + self.stylename.inspect else suffix = '' end if [:inner].is_a? Hash inner = '' [:inner].each do |key, value| inner += ', ' if inner.length > 0 inner += "#{key}: #{value.inspect}" end else inner = [:inner] end "#{self.class.name}(##{self.object_id.to_s(16)}, #{SugarCube::Adjust::format_frame(self.frame)}" + (inner ? ', ' + inner : '') + ')' + ([:superview] && self.superview ? ", child of #{self.superview.class.name}(##{self.superview.object_id.to_s(16)})" : '') + suffix end |
#unshift(view) ⇒ Object
45 46 47 48 |
# File 'lib/sugarcube/uiview.rb', line 45 def unshift(view) self.insertSubview(view, atIndex:0) return self end |