Last active
April 22, 2025 09:09
-
-
Save JaviSoto/6926083 to your computer and use it in GitHub Desktop.
Rotation transformation with anchor point
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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