class MotionKit::Constraint
Constants
- Attributes
- Priorities
- Relationships
Attributes
attribute[RW]
attribute2[RW]
constant[R]
identifier[W]
multiplier[R]
priority[W]
relationship[RW]
relative_to[RW]
target[RW]
Public Class Methods
attribute_lookup(attribute)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 376 def attribute_lookup(attribute) if attribute.is_a? Fixnum return attribute end unless Attributes.key? attribute raise InvalidAttributeError.new("Unsupported attribute #{attribute.inspect}") end Attributes[attribute] end
attribute_reverse(attribute)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 412 def attribute_reverse(attribute) Attributes.key(attribute) || :none end
axis_lookup(axis)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 342 def axis_lookup(axis) case axis when :horizontal UILayoutConstraintAxisHorizontal when :vertical UILayoutConstraintAxisVertical else axis end end
calculate?(value)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 420 def calculate?(value) value.is_a?(String) end
constant?(value)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 424 def constant?(value) value.is_a?(Numeric) || value.is_a?(Hash) || value.is_a?(Array) end
new(target, attribute=nil, relationship)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 48 def initialize(target, attribute=nil, relationship) @target = target @attribute = attribute @attribute2 = attribute @relative_to = nil @relationship = relationship @multiplier = 1 @constant = 0 @priority = nil @compare_flag = false @active = true end
orientation_lookup(orientation)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 353 def orientation_lookup(orientation) case orientation when :horizontal NSLayoutConstraintOrientationHorizontal when :vertical NSLayoutConstraintOrientationVertical else orientation end end
priority_lookup(priority)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 388 def priority_lookup(priority) if priority.is_a? Fixnum return priority end unless Priorities.key? priority raise InvalidPriorityError.new("Unsupported priority #{priority.inspect}") end Priorities[priority] end
relationship_lookup(relationship)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 400 def relationship_lookup(relationship) if relationship.is_a? Fixnum return relationship end unless Relationships.key? relationship raise InvalidRelationshipError.new("Unsupported relationship #{relationship.inspect}") end Relationships[relationship] end
relationship_reverse(relationship)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 416 def relationship_reverse(relationship) Relationships.key(relationship) end
view_lookup(layout, view, target)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 364 def view_lookup(layout, view, target) if ! target || target.is_a?(MotionKit.base_view_class) target elsif target == :self view elsif target == :superview view.superview else layout.get_view(target) end end
Public Instance Methods
<=(compare)
click to toggle source
Calls superclass method
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 99 def <=(compare) if @compare_flag lte(compare) self else super end end
==(compare)
click to toggle source
Calls superclass method
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 79 def ==(compare) if @compare_flag equals(compare) self else super end end
>=(compare)
click to toggle source
Calls superclass method
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 89 def >=(compare) if @compare_flag gte(compare) self else super end end
activate()
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 294 def activate self.active = true self end
active()
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 274 def active @active end
active=(active)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 278 def active=(active) if @resolved if active @resolved.each do |constraint| common_ancestor.addConstraint(constraint) end else @resolved.each do |constraint| common_ancestor.removeConstraint(constraint) end end else @active = active end end
common_ancestor()
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 304 def common_ancestor if @resolved @common_ancestor ||= begin constraint = @resolved[0] base_view_class = MotionKit.base_view_class if constraint.firstItem.is_a?(base_view_class) && constraint.secondItem.is_a?(base_view_class) common_ancestor = nil ancestors = [constraint.firstItem] parent_view = constraint.firstItem while parent_view = parent_view.superview ancestors << parent_view end current_view = constraint.secondItem while current_view if ancestors.include? current_view common_ancestor = current_view break end current_view = current_view.superview end unless common_ancestor raise NoCommonAncestorError.new("No common ancestors between #{constraint.firstItem} and #{constraint.secondItem}") end else common_ancestor = constraint.firstItem end common_ancestor end end end
constant=(constant)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 178 def constant=(constant) @constant = constant case Constraint.attribute_lookup(self.attribute) when NSLayoutAttributeLeft, NSLayoutAttributeRight, NSLayoutAttributeTop, NSLayoutAttributeBottom, NSLayoutAttributeLeading, NSLayoutAttributeTrailing, NSLayoutAttributeCenterX, NSLayoutAttributeCenterY, NSLayoutAttributeBaseline self.relative_to ||= :superview end self.update_constraint end
deactivate()
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 299 def deactivate self.active = false self end
divided_by(multiplier)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 199 def divided_by(multiplier) times 1.0/multiplier end
equals(target, attribute2=nil)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 109 def equals(target, attribute2=nil) self.relationship ||= :equal if Constraint.constant?(target) self.constant = target elsif Constraint.calculate?(target) calc = Calculator.scan(target) self.multiplier = calc.factor self.constant = calc.constant || 0 else self.relative_to = target if attribute2 self.attribute2 = attribute2 end end @compare_flag = false self end
Also aliased as: is_equal_to, equal_to
gte(target, attribute2=nil)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 149 def gte(target, attribute2=nil) self.relationship = :gte if Constraint.constant?(target) self.constant = target elsif Constraint.calculate?(target) calc = Calculator.scan(target) self.multiplier = calc.factor self.constant = calc.constant || 0 else self.relative_to = target if attribute2 self.attribute2 = attribute2 end end @compare_flag = false self end
Also aliased as: is_at_least, at_least
identifier(identifier=nil)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 229 def identifier(identifier=nil) return @identifier if identifier.nil? @identifier = identifier self end
Also aliased as: identified_by
is(value=nil)
click to toggle source
like `equals`, but also sets `compare_flag` to true, so you can use ==, <=, and >=
@example
x.is(10) x.is(10).priority(:required) x.is == 10 (x.is == 10).priority :required width.is >= 100 height.is <= 200
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 71 def is(value=nil) if value self.equals(value) end @compare_flag = true self end
lte(target, attribute2=nil)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 129 def lte(target, attribute2=nil) self.relationship = :lte if Constraint.constant?(target) self.constant = target elsif Constraint.calculate?(target) calc = Calculator.scan(target) self.multiplier = calc.factor self.constant = calc.constant || 0 else self.relative_to = target if attribute2 self.attribute2 = attribute2 end end @compare_flag = false self end
Also aliased as: is_at_most, at_most
minus(constant)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 218 def minus(constant) plus -constant end
multiplier=(multiplier)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 189 def multiplier=(multiplier) @multiplier = multiplier self.update_constraint end
of(target, attribute2=nil)
click to toggle source
width.is('100%').of(:view, :width)
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 170 def of(target, attribute2=nil) self.relative_to = target if attribute2 self.attribute2 = attribute2 end self end
plus(constant)
click to toggle source
If no relationship has been set, the “use case” here is:
c.plus(10).equals(:view, :x)
Which is the same as
c.equals(:view, :x).minus(10)
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 210 def plus(constant) unless self.relationship constant = -constant end self.constant += constant self end
priority(priority=nil)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 222 def priority(priority=nil) return @priority if priority.nil? @priority = priority self end
resolve_all(layout, view)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 245 def resolve_all(layout, view) @resolved ||= begin item = Constraint.view_lookup(layout, view, self.target) rel_item = Constraint.view_lookup(layout, view, self.relative_to) relationship = Constraint.relationship_lookup(self.relationship) attribute1 = Constraint.attribute_lookup(self.attribute) attribute2 = Constraint.attribute_lookup(self.attribute2) nsconstraint = NSLayoutConstraint.constraintWithItem(item, attribute: attribute1, relatedBy: relationship, toItem: rel_item, attribute: attribute2, multiplier: self.multiplier, constant: self.constant ) if self.priority nsconstraint.priority = Constraint.priority_lookup(self.priority) end if self.identifier nsconstraint.setIdentifier(self.identifier) end [nsconstraint] end end
times(multiplier)
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 194 def times(multiplier) self.multiplier *= multiplier self end
update_constraint()
click to toggle source
# File lib/motion-kit-cocoa/constraints/constraint.rb, line 237 def update_constraint if @resolved constraint = @resolved[0] constraint.constant = self.constant end end