Skip to content

Instantly share code, notes, and snippets.

@JaviSoto
Last active April 22, 2025 09:09
Show Gist options
  • Save JaviSoto/6926083 to your computer and use it in GitHub Desktop.
Save JaviSoto/6926083 to your computer and use it in GitHub Desktop.
Rotation transformation with anchor point
- (void)rotateView:(UIView *)view
byRadianDegrees:(CGFloat)radianDegrees
withAnchorPoint:(CGPoint)relativeAnchorPoint
{
const CGRect viewBounds = view.bounds;
const CGPoint anchorPoint = CGPointMake(viewBounds.size.width * relativeAnchorPoint.x, viewBounds.size.height * relativeAnchorPoint.y);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, anchorPoint.x, anchorPoint.y);
transform = CGAffineTransformRotate(transform, radianDegrees);
transform = CGAffineTransformTranslate(transform, -anchorPoint.x, -anchorPoint.y); // Translate back
view.transform = transform;
}
- (void)example
{
[self rotateView:self.view
byRadianDegrees:(CGFloat)M_PI_2
withAnchorPoint:CGPointMake(0.5f, 0.0f)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment