Skip to content

Instantly share code, notes, and snippets.

@cbowns
Created June 26, 2012 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbowns/2992390 to your computer and use it in GitHub Desktop.
Save cbowns/2992390 to your computer and use it in GitHub Desktop.
Helper function for moving UIView/CALayer centers when changing anchorPoints
- (CGPoint)center:(CGPoint)oldCenter movedFromAnchorPoint:(CGPoint)oldAnchorPoint toAnchorPoint:(CGPoint)newAnchorPoint withFrame:(CGRect)frame;
{
CGPoint anchorPointDiff = CGPointMake(newAnchorPoint.x - oldAnchorPoint.x, newAnchorPoint.y - oldAnchorPoint.y);
CGPoint newCenter = CGPointMake(oldCenter.x + (anchorPointDiff.x * frame.size.width),
oldCenter.y + (anchorPointDiff.y * frame.size.height));
return newCenter;
}
@cbowns
Copy link
Author

cbowns commented Jul 12, 2012

Example usage:

CGPoint newTopViewAnchorPoint = CGPointMake(0.5, 1.0);
CGPoint newTopViewCenter = [self center:topHalfFrontView.center movedFromAnchorPoint:topHalfFrontView.layer.anchorPoint toAnchorPoint:newTopViewAnchorPoint withFrame:topHalfFrontView.frame];
topHalfFrontView.layer.anchorPoint = newTopViewAnchorPoint;
topHalfFrontView.center = newTopViewCenter;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment