Skip to content

Instantly share code, notes, and snippets.

@cbowns
Created June 26, 2012 00:50
Show Gist options
  • Select an option

  • Save cbowns/2992390 to your computer and use it in GitHub Desktop.

Select an option

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

cbowns commented Jul 12, 2012

Copy link
Copy Markdown
Author

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