Class: UIView
- Inherits:
-
Object
- Object
- UIView
- Includes:
- Teacup::Layout
- Defined in:
- lib/teacup/z_core_extensions/ui_view.rb,
lib/teacup/z_core_extensions/ui_view_getters.rb
Overview
Methods to retrieve a subview using the stylename as a key Kinda similar to jQuery-style $().find(‘stylename’)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#debug ⇒ Object
Enable debug messages for this object.
-
#stylename ⇒ Object
The current stylename that is used to look up properties in the stylesheet.
Instance Method Summary collapse
- #add_uniq_constraints(constraint) ⇒ Object
-
#animate_to_style(style) ⇒ Object
Animate a change to new styles.
-
#animate_to_stylename(stylename, options = {}) ⇒ Object
Animate a change to a new stylename.
- #apply_constraints ⇒ Object
- #get_ns_constraints ⇒ Object
- #restyle!(orientation = nil) ⇒ Object
- #set_stylesheet_quickly(new_stylesheet) ⇒ Object
-
#style(properties, orientation = nil) ⇒ Object
Apply style properties to this element.
-
#stylesheet=(new_stylesheet) ⇒ Object
Alter the stylesheet of this view.
- #top_level_view ⇒ Object
-
#viewsWithStylename(name_or_class) ⇒ Object
get all subviews by stylename or class my_view.viewsWithStylename :button => [#<UIButton..>, #<UIButton…>] my_view.viewsWithStylename UIButton => [#<UIButton..>, #<UIButton…>].
-
#viewWithStylename(name_or_class) ⇒ Object
get one subview by stylename or class my_view.viewWithStylename :button => #<UIButton..> my_view.viewWithStylename UIButton => #<UIButton..>.
Methods included from Teacup::Layout
#layout, #stylesheet, #subview
Instance Attribute Details
#debug ⇒ Object
Enable debug messages for this object
13 14 15 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 13 def debug @debug end |
#stylename ⇒ Object
The current stylename that is used to look up properties in the stylesheet.
10 11 12 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 10 def stylename @stylename end |
Instance Method Details
#add_uniq_constraints(constraint) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 207 def add_uniq_constraints(constraint) @teacup_constraints ||= {} if constraint.is_a? Array constraint.each { |constraint| add_uniq_constraints(constraint) } elsif constraint.is_a? Hash constraint.each { |sym, relative_to| @teacup_constraints[sym] = relative_to } elsif constraint.is_a? Teacup::Constraint or constraint.is_a? Symbol @teacup_constraints[constraint] = true else raise "Unsupported constraint: #{constraint.inspect}" end end |
#animate_to_style(style) ⇒ Object
Animate a change to new styles
This is equivalent to wrapping a call to .style() inside UIView.beginAnimations.
171 172 173 174 175 176 177 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 171 def animate_to_style(style) UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(style[:duration]) if style[:duration] UIView.setAnimationCurve(style[:curve]) if style[:curve] style(style) UIView.commitAnimations end |
#animate_to_stylename(stylename, options = {}) ⇒ Object
Animate a change to a new stylename.
This is equivalent to wrapping a call to .stylename= inside UIView.beginAnimations.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 153 def animate_to_stylename(stylename, ={}) return if self.stylename == stylename UIView.beginAnimations(nil, context: nil) # TODO: This should be in a style-sheet! UIView.setAnimationDuration([:duration]) if [:duration] UIView.setAnimationCurve([:curve]) if [:curve] self.stylename = stylename UIView.commitAnimations end |
#apply_constraints ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 126 def apply_constraints if @teacup_added_constraints @teacup_added_constraints.each do |constraint| self.removeConstraint(constraint) end end @teacup_added_constraints = nil all_constraints = get_ns_constraints return if all_constraints.empty? @teacup_added_constraints = [] all_constraints.each do |ns_constraint| @teacup_added_constraints << ns_constraint self.addConstraint(ns_constraint) end end |
#get_ns_constraints ⇒ Object
61 62 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 61 def get_ns_constraints # gets the array of Teacup::Constraint objects my_constraints = (@teacup_constraints || []).map { |constraint, relative_to| if constraint.is_a?(Teacup::Constraint) constraint else if relative_to == true Teacup::Constraint.from_sym(constraint) else Teacup::Constraint.from_sym(constraint, relative_to) end end }.flatten.tap{ |my_constraints| unless my_constraints.empty? self.setTranslatesAutoresizingMaskIntoConstraints(false) end }.map do |original_constraint| constraint = original_constraint.copy case original_constraint.target when UIView constraint.target = original_constraint.target when :self constraint.target = self when :superview constraint.target = self.superview when Symbol, String container = self constraint.target = nil while container && constraint.target.nil? constraint.target = container.viewWithStylename(original_constraint.target) container = container.superview end end case original_constraint.relative_to when UIView constraint.relative_to = original_constraint.relative_to when :self constraint.relative_to = self when :superview constraint.relative_to = self.superview when Symbol, String # TODO: this re-checks lots of views - everytime it goes up to the # superview, it checks all the leaves again. container = self constraint.relative_to = nil while container && constraint.relative_to.nil? constraint.relative_to = container.viewWithStylename(original_constraint.relative_to) container = container.superview end end # the return value, for the map constraint.nslayoutconstraint end # now add all che child constraints subviews.each do |subview| my_constraints.concat(subview.get_ns_constraints) end my_constraints end |
#restyle!(orientation = nil) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 52 def restyle!(orientation=nil) if Teacup.should_restyle? if stylesheet && stylesheet.is_a?(Teacup::Stylesheet) style(stylesheet.query(stylename, self, orientation)) end subviews.each{ |subview| subview.restyle!(orientation) } end end |
#set_stylesheet_quickly(new_stylesheet) ⇒ Object
47 48 49 50 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 47 def set_stylesheet_quickly(new_stylesheet) @stylesheet = new_stylesheet subviews.each{ |subview| subview.set_stylesheet_quickly(new_stylesheet) } end |
#style(properties, orientation = nil) ⇒ Object
Apply style properties to this element.
Takes a hash of properties such as may have been read from a stylesheet or passed as parameters to Teacup::Layout#layout, and applies them to the element.
Does a little bit of magic (that may be split out as ‘sugarcube’) to make properties work as you’d expect.
If you try and assign something in properties that is not supported, a warning message will be emitted.
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 192 def style(properties, orientation=nil) if properties.key?(:constraints) add_uniq_constraints(properties.delete(:constraints)) end Teacup.apply_hash self, properties self.setNeedsDisplay self.setNeedsLayout end |
#stylesheet=(new_stylesheet) ⇒ Object
Alter the stylesheet of this view.
This will cause new styles to be applied using the current stylename, and will recurse into subviews.
If you would prefer that a given UIView object does not inherit the stylesheet from its parents, override the ‘stylesheet’ method to return the correct value at all times.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 35 def stylesheet=(new_stylesheet) should_restyle = Teacup.should_restyle_and_block @stylesheet = new_stylesheet subviews.each{ |subview| subview.set_stylesheet_quickly(new_stylesheet) } if should_restyle Teacup.should_restyle! restyle! end end |
#top_level_view ⇒ Object
203 204 205 |
# File 'lib/teacup/z_core_extensions/ui_view.rb', line 203 def top_level_view return self end |
#viewsWithStylename(name_or_class) ⇒ Object
get all subviews by stylename or class my_view.viewsWithStylename :button => [#<UIButton..>, #<UIButton…>] my_view.viewsWithStylename UIButton => [#<UIButton..>, #<UIButton…>]
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/teacup/z_core_extensions/ui_view_getters.rb', line 28 def viewsWithStylename name_or_class r = [] subviews.each do |view| if name_or_class.is_a? Class if view.is_a? name_or_class r << view end elsif view.stylename == name_or_class r << view end r.concat view.viewsWithStylename name_or_class end r end |
#viewWithStylename(name_or_class) ⇒ Object
get one subview by stylename or class my_view.viewWithStylename :button => #<UIButton..> my_view.viewWithStylename UIButton => #<UIButton..>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/teacup/z_core_extensions/ui_view_getters.rb', line 8 def viewWithStylename name_or_class if name_or_class.is_a? Class view = subviews.find { |view| view.is_a? name_or_class } else view = subviews.find { |view| view.stylename == name_or_class } end return view if view # found_subview will get assigned to the view we want, but the subview is # what is returned. found_subview = nil view = subviews.find {|subview| found_subview = subview.viewWithStylename(name_or_class) } return found_subview if view nil # couldn't find it end |