Class: UIView
- Inherits:
-
Object
- Object
- UIView
- Defined in:
- lib/cha_work/sugar/uiview.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(view) ⇒ Object
superview << view => superview.addSubview(view).
- #hide ⇒ Object
- #off_gestures ⇒ 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_press_begin(duration_or_options = nil, &proc) ⇒ Object
- #on_rotate {|recognizer| ... } ⇒ Object
- #on_swipe(direction_or_options = nil) {|recognizer| ... } ⇒ Object
- #on_tap(taps_or_options = nil) {|recognizer| ... } ⇒ Object
- #show ⇒ Object
-
#uiimage(use_content_size = false) ⇒ Object
Easily take a snapshot of a ‘UIView`.
Class Method Details
.attr_updates(*attrs) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cha_work/sugar/uiview.rb', line 5 def attr_updates(*attrs) attr_accessor(*attrs) attrs.each do |attr| define_method("#{attr}=") do |value| if instance_variable_get("@#{attr}") != value setNeedsDisplay end willChangeValueForKey(attr) instance_variable_set("@#{attr}", value) didChangeValueForKey(attr) end end end |
Instance Method Details
#<<(view) ⇒ Object
superview << view
> superview.addSubview(view)
23 24 25 26 |
# File 'lib/cha_work/sugar/uiview.rb', line 23 def <<(view) self.addSubview(view) return self end |
#hide ⇒ Object
34 35 36 37 |
# File 'lib/cha_work/sugar/uiview.rb', line 34 def hide self.hidden = true return self end |
#off_gestures ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cha_work/sugar/uiview.rb', line 105 def off_gestures if @sugarcube_recognizers @sugarcube_recognizers.each do |recognizer, proc| self.removeGestureRecognizer(recognizer) end @sugarcube_recognizers = nil end self end |
#on_gesture(recognizer) ⇒ Object #on_gesture(recognizer_class) ⇒ Object
A generic gesture adder, but accepts a block like the other gesture methods
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cha_work/sugar/uiview.rb', line 91 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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/cha_work/sugar/uiview.rb', line 196 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
142 143 144 145 |
# File 'lib/cha_work/sugar/uiview.rb', line 142 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
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/cha_work/sugar/uiview.rb', line 228 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_press_begin(duration_or_options = nil, &proc) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/cha_work/sugar/uiview.rb', line 250 def on_press_begin(=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_long_press_on_begin:') 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
148 149 150 151 |
# File 'lib/cha_work/sugar/uiview.rb', line 148 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/cha_work/sugar/uiview.rb', line 159 def on_swipe(=nil, &proc) direction = nil 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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cha_work/sugar/uiview.rb', line 122 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 |
#show ⇒ Object
29 30 31 32 |
# File 'lib/cha_work/sugar/uiview.rb', line 29 def show self.hidden = false return self end |
#uiimage(use_content_size = false) ⇒ Object
Easily take a snapshot of a ‘UIView`.
Calling ‘uiimage` with no arguments will return the image based on the `bounds` of the image. In the case of container views (notably `UIScrollView` and its children) this does not include the entire contents, which is something you probably want.
If you pass a truthy value to this method, it will use the ‘contentSize` of the view instead of the `bounds`, and it will draw all the child views, not just those that are visible in the viewport.
It is guaranteed that ‘true` and `:all` will always have this behavior. In the future, if this argument becomes something that accepts multiple values, those two are sacred.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cha_work/sugar/uiview.rb', line 53 def uiimage(use_content_size=false) if use_content_size UIGraphicsBeginImageContextWithOptions(contentSize, false, 0.0) context = UIGraphicsGetCurrentContext() self.subviews.each do |subview| CGContextSaveGState(context) CGContextTranslateCTM(context, subview.frame.origin.x, subview.frame.origin.y) subview.layer.renderInContext(context) CGContextRestoreGState(context) end image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() else UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0) if self.respond_to?('drawViewHierarchyInRect:afterScreenUpdates:') self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true) else layer.renderInContext(UIGraphicsGetCurrentContext()) end image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() end return image end |