Skip to content

Instantly share code, notes, and snippets.

@dblandin
Created October 30, 2013 18:29
Show Gist options
  • Save dblandin/7237555 to your computer and use it in GitHub Desktop.
Save dblandin/7237555 to your computer and use it in GitHub Desktop.
Erica Sadun's UIView-Transform category http://www.informit.com/articles/article.aspx?p=1951182
module UIViewAdditions
def offsetPointToParentCoordinates(aPoint)
CGPointMake(aPoint.x + center.x, aPoint.y + center.y)
end
def pointInViewCenterTerms(aPoint)
CGPointMake(aPoint.x - center.x, aPoint.y - center.y)
end
def pointInTransformedView(aPoint)
offsetItem = pointInViewCenterTerms(aPoint)
updatedItem = CGPointApplyAffineTransform(offsetItem, transform)
finalItem = offsetPointToParentCoordinates(updatedItem)
finalItem
end
def originalFrame
currentTransform = transform
self.transform = CGAffineTransformIdentity
originalFrame = frame
self.transform = currentTransform
originalFrame
end
def transformedTopLeft
frame = originalFrame
point = frame.origin
pointInTransformedView(point)
end
def transformedTopRight
frame = originalFrame
point = frame.origin
point.x += frame.size.width
pointInTransformedView(point)
end
def transformedBottomRight
frame = originalFrame
point = frame.origin
point.x += frame.size.width
point.y += frame.size.height
pointInTransformedView(point)
end
def transformedBottomLeft
frame = originalFrame
point = frame.origin
point.y += frame.size.height
pointInTransformedView(point)
end
end
UIView.send(:include, UIViewAdditions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment