Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

Geomotion for RubyMotion

iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?

Features

  • Adds methods to return useful information, like whether a rect.contains?(a_point), or point.distance_to(another_point)
  • Easily modify CGRects with methods like shrink_left, grow_down, below, and many many more.
  • Easy conversion to and from NSValue (#to_ns_value and ##from_ns_value)
  • Adds nice inspect methods
  • Many operators (+, -, *)
  • CATransform3D and CGAffineTransform methods to create and concatenate transforms
  • Read on for in-depth examples!

CGRect

# Initializersrect=CGRect.new([10,100],[50,20])rect=CGRect.make(x: 10,y: 100,width: 50,height: 20)rect=CGRect.make(origin: CGPoint(0,0),size: CGSize(0,0))# there are, for convenience, function versions of these:rect=CGRect(10,100,50,20)rect=CGRect([10,100],[50,20])rect=CGRect([[10,100],[50,20]])rect=CGRect(x: 10,y: 100,w: 50,h: 20)rect=CGRect(origin: [10,100],size: [50,20])# Getters[rect.x,rect.y,rect.width,rect.height]=>[10,100,50,20]goofy_rect=CGRect.make(x: 10.1,y: 100.9,width: 50.2,height: 20.8)goofy_rect.integral=>CGRect([10.0,100.0],[51.0,22.0])rect_zero=CGRect.zerorect_zero=CGRect.empty# alias for CGRect.zero=>CGRect(0,0,0,0)rect_zero.empty?=>true# to get the center of the frame, relative to the origin or in absolute coordinatesrect.center=>CGPoint(25,10)# center relative to boundsrect.center(true)=>CGPoint(35,110)# center relative to frame# length of the diagonalsrect=CGRect.new([0,0],[30,40])rect.diagonal# => 50# Other points in the rect can be returned as well, and the same# relative/absolute return values are supported (defaults to relative)top_lefttop_center
| |
o--o--otop_right
| |
center_leftoxocenter_right
| |
o--o--obottom_right
| |
bottom_leftbottom_center# Operator Overloading
-rect=>CGRect(-10, -100, -50, -20)# union of rectsrect + CGRect.make(x: 9,y: 99,width: 10,height: 10)=>rect.union_with(CGRect.make(x: 9,y: 99,width: 10,height: 10))=>CGRect(9,99,50,20)# increases the size, but keeps the originrect + CGSize.make(width: 11,height: 1)=>CGRect(10,100,61,21)# not the same as `grow`, which grows the rect in all directions# move the rect via a pointrect + CGPoint.make(x: 10,y: 10)=>rect.offset(CGPoint.make(x: 10,y: 10))=>CGRect(20,110,50,20)# move the rect via an offsetrect + UIOffsetMake(10,10)rect.offset(UIOffsetMake(10,10))rect.offset(10,10)=>CGRect(20,110,50,20)a_point + a_size=>CGRect(a_point,a_size)# a point and a size make a rectangle. makes sense, right?# Union and Intersectionrect.union_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(9,99,50,20)rect.intersection_withCGRect.make(x: 9,y: 99,width: 10,height: 10)=>CGRect(10,100,10,10)rect.intersects?(another_rect)=>true/false,whethertheyoverlapatallor not
rect.contains?(a_pointora_rect)=>true/false,whetherthepointorrectis *completelycontained* inthereceivingrect# Growing and shrinking# The center stays the same. Think margins!rect.grow(CGSize.make(width: 10,height: 20))=>CGRect(5,90,60,40)rect.shrink(10)=>CGRect(15,105,40,10)# Powerful layout adjustments with chainable methodsview=UIView.alloc.initWithFramerect.below.width(100).height(10)view.frame=>CGRect(10,120,100,10)view2=UIView.alloc.initWithFramerect.beside(10)view2.frame=>CGRect(70,100,50,20.0)# More examples of adjustmentsrect=CGRect.make(x: 10,y: 100,width: 50,height: 20)[rect.right(20).x,rect.left(20).x,rect.up(20).y,rect.down(20).y]=>[30, -10,80,120]# Layout "above" and "before" rectangles# (default offset is the rectangle's width or height)rect.before(5)=>CGRect(-45,100,50,20)rect.before(5,width:20)=>CGRect(-15,100,20,20)rect.above(5)=>CGRect(10,75,50,20)rect.above(5,height:10)=>CGRect(10,85,50,10)# Layout a rect relative to othersrect2=CGRect.make(x: 50,y: 50,width: 100,height: 100)rect3=CGRect.make(x:100,y: 200,width: 20,height: 20)CGRect.layout(rect,above: rect2,right_of: rect3)=>CGRect(120,30,50,20)# Also supports marginsCGRect.layout(rect,above: rect2,right_of: rect3,margins: [0,0,10,15])=>CGRect(135,20,50,20)

Relative vs Absolute

When you are positioning frames, you'll be doing so in one of two ways:

  1. Two frames relative to each other, within a common parent frame
  2. A frame being added as a child of another frame

(generally speaking)

geomotion is optimized for both cases, but the arsenal of methods is different.

frames relative to each other

Any of the location methods (up, down, left, right, beside, before, above, below) will return a frame that is in the same coordinate system of the receiver, and this behavior cannot be changed.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside# => [[20, 10], [10, 10]]frame.right(30).down(5).taller(100)# => [[10+30, 10+5], [10, 10+100]]# aka# => [[40, 15], [10, 110]]

Any methods that include the x or y variable in their name will be absolute.

frame.xframe.min_xframe.max_xframe.mid_xframe.yframe.min_yframe.max_yframe.mid_y
positions relative to the frame's origin

Any of the position methods that do NOT include the x or y variable will ignore the x and y values unless explicitly told to use absolute coordinates.

Note: These methods will "normalize" the width and height, so even if the width or height is negative, these methods will always return positive numbers. If you specify absolute coordinates, the values might be negative, but they will also be sorted (x == min, min < mid, mid < max, x + width == max).

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.top_left# => [0, 0]frame.top_center# => [5, 0]frame.bottom_right# => [10, 10]# use absolute coordinatesframe.top_left(true)# => [10, 10]frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]# negative widths and heights are "corrected" when using absolute coordinatesframe=CGRect.make(x: 20,y: 20,width:-10,height: -10)frame.top_center(true)# => [15, 10]frame.bottom_right(true)# => [20, 20]

The great and powerful apply method

Most of the frame-manipulation methods delegate to the apply method. You can use this method to perform batch changes.

frame=view.frame.apply(left: 10,y: 0,wider: 50,grow_height: 10)

All of the methods that return a new frame (left, shrink, below and friends) also accept a hash in which you can apply more changes. You can accomplish the same thing using method chaining; this is an implementation detail that might also clean your code up by grouping changes.

frame=CGRect.make(x: 10,y: 10,width:10,height: 10)frame.beside.width(20).down(10).height(20)# => [[20, 20], [20, 20]]# using the options hash / apply methodframe.beside(width: 20,down: 10,height: 20)# => [[20, 20], [20, 20]]frame.below(grow_width: 10,grow_up: 5)# => [[0, 15], [40, 25]]# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed. CGRect is a "boxed" object in RubyMotion, and in Objective-C it is a# C-struct and so can't be stored in an NSArray, for example.NSValue.valueWithCGRect(CGRect.new([0,10],[10,20]))# =>value=CGRect.new([0,10],[10,20]).to_ns_valuerect=CGRect.from_ns_value(value)

CGSize

# Initializerssize=CGSize.new(50,20)size=CGSize.make(width: 50,height: 20)# there are, for convenience, function versions of these:size=CGSize(50,20)size=CGSize([50,20])size=CGSize(width: 50,height: 20)# Getterssize_zero=CGSize.empty=>CGSize(0,0)size_zero.empty?=>true# length of the diagonalssize=CGSize.new([30,40])size.diagonal# => 50# modify width, height, or both# biggersize_zero=CGSize.emptysize_zero.grow(5)# => CGSize(5, 5)size_zero.wider(10)# => CGSize(10, 0)size_zero.taller(10)# => CGSize(0, 10)# smallersize_ten=CGSize.new(10,10)size_ten.shrink(5)# => CGSize(5, 5)size_ten.shorter(10)# => CGSize(10, 0)size_ten.thinner(10)# => CGSize(0, 10)# Operator Overloading
-size=>CGSize(-50, -20)size + CGSize.make(width: 100,height: 50)=>CGSize(150,70)size + CGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# Combine with CGPointsize.rect_at_pointCGPoint.make(x: 10,y: 30)=>CGRect(10,30,50,20)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGSize(CGSize.new(0,10))# =>value=CGSize.new(0,10).to_ns_valuesize=CGSize.from_ns_value(value)

CGPoint

# Initializerspoint=CGPoint.new(10,100)point=CGPoint.make(x: 10,y: 100)# there are, for convenience, function versions of these:point=CGPoint(10,100)point=CGPoint([10,100])point=CGPoint(x: 10,y: 100)# Return a modified copypoint.up(50).left(5)=>CGPoint(5,50)# original is not modified, a new point is returnedpoint.down(50).right(5)=>CGPoint(15,150)# Operator Overloading
-point=>CGPoint(-10, -100)point + CGPoint.make(x: 20,y: 40)=>CGPoint(30,140)point + CGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Combine with CGSizepoint.rect_of_sizeCGSize.make(width: 50,height: 20)=>CGRect(10,100,50,20)# Compare with CGRectpoint.inside?CGRect.make(x: 0,y: 0,width: 20,height: 110)=>true# Compare with origin# lengthCGPoint.new(3,4).length=>5# angleCGPoint.new(1,1).angle * 180 / Math::PI=>45.0# if you only need to *compare* lengths, use rough_length. It is faster, since# it doesn't perform the sqrt part of pythagorean's theorem.CGPoint.new(3,4).rough_length=>25# Distance to pointpoint=CGPoint.new(10,100)point.distance_to(CGPoint.make(x: 13,y:104))=>5# If you just need to know whether the points are within a certain distance, it# is faster to use distance_within? (it uses rough_length to compare the distances)point.distance_within?(5,to: CGPoint.make(x: 13,y: 104))=>true# Angle between target and receiver# (hint: our answer should be 45°)point=CGPoint.new(10,100)point.angle_to(CGPoint.make(x: 20,y:110))=>0.785398163397(pi/4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGPoint(CGPoint.new(0,10))# =>value=CGPoint.new(0,10).to_ns_valuepoint=CGPoint.from_ns_value(value)

CGAffineTransform

These are assigned to the UIView#transform parameter. See CATransform3D for the transforms that are designed for CALayer object.

# you *can* create it manuallytransform=CGAffineTransform.make(a: 1,b: 0,c: 0,d: 1,tx: 0,ty: 0)transform=CGAffineTransform(1,0,0,1,0,0)# but don't! the `make` method accepts `translate`, `scale`, and `rotate` argstransform=CGAffineTransform.make(scale: 2,translate: [10,10],rotate: Math::PI)# identity transform is easyCGAffineTransform.identity# just to be sureCGAffineTransform.identity.identity?# => true# Operator Overloadingtransform1=CGAffineTransform.make(scale: 2)transform2=CGAffineTransform.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CGAffineTransform.identity# create new transforms by calling `translate`, `scale`, or `rotate` as factory# methodsCGAffineTransform.translate(10,10)CGAffineTransform.scale(2)# scale x and y by 2CGAffineTransform.scale(2,4)# scale x by 2 and y by 4CGAffineTransform.rotate(Math::PI / 4)# "shearing" turns a rectangle into a parallelogram# see sceenshot below or run geomotion appCGAffineTransform.shear(0.5,0)# in x directionCGAffineTransform.shear(0,0.5)# in y direction# you can combine these, but it looks kind of strange. better to pick one# direction# or you can chain these methodsCGAffineTransform.identity.translate(10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or where an Objective-C object is# needed.NSValue.valueWithCGAffineTransform(CGAffineTransform.translate(0,10))# =>value=CGAffineTransform.translate(0,10).to_ns_valuetransform=CGAffineTransform.from_ns_value(value)
Shearing

Shearing

CATransform3D

CALayers can take on full 3D transforms.

# these are really gnarlytransform=CATransform3D.make(m11: 1,m12: 0,m13: 0,m14: 0,m21: 0,m22: 1,m23: 0,m24: 0,m31: 0,m32: 0,m33: 1,m34: 0,m41: 0,m42: 0,m43: 0,m44: 1,)transform=CATransform3D(samethingworkshere)# accepts transforms like CGAffineTransform, but many take 3 instead of 2 argstransform=CATransform3D.make(scale: [2,2,1],translate: [10,10,10],rotate: Math::PI)# identity transformCATransform3D.identityCATransform3D.identity.identity?# => true# Operator Overloadingtransform1=CATransform3D.make(scale: 2)transform2=CATransform3D.make(translate: [10,10])# concatenate transformstransform1 + transform2transform1 << transform2# aliastransform1 - transform2# => transform1 + -transform2# => transform1 + transform2.inverttransform1 - transform1# => CATransform3D.identity# create new transforms by calling factory methodsCATransform3D.translate(10,10,10)CATransform3D.scale(2)# scale x and y by 2CATransform3D.scale(2,4,3)# scale x by 2, y by 4, z by 3CATransform3D.rotate(Math::PI / 4)# "shearing" works the same as CGAffineTransformCATransform3D.shear(0.5,0)# in x directionCATransform3D.shear(0,0.5)# in y direction# "perspective" changes are better than rotation because they make one side# bigger and one side smaller# see sceenshot below or run geomotion appCATransform3D.perspective(0.002,0)# similar to rotating around x-axisCATransform3D.perspective(0,0.002)# "rotates" around the y-axis# or you can chain these methodsCATransform3D.identity.translate(10,10,10).scale(2).rotate(Math::PI / 4)# convert to NSValue, for use in NSCoding or CAKeyframeAnimation#valuesNSValue.valueWithCATransform3D(CATransform3D.translate(0,10,0))# =>value=CATransform3D.translate(0,10,0).to_ns_valuetransform=CATransform3D.from_ns_value(value)
Perspective

Perspective

Install

  1. gem install geomotion

  2. Add require 'geomotion' in your Rakefile.

Forking

If you have cool/better ideas, pull-request away!

About

Better iOS Geometry with RubyMotion

Resources

Stars

90 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages