Skip to content

Instantly share code, notes, and snippets.

@edwardean
Created April 1, 2016 15:29
Show Gist options
  • Save edwardean/6045f4de846b221fdd4cb14057e43d0f to your computer and use it in GitHub Desktop.
Save edwardean/6045f4de846b221fdd4cb14057e43d0f to your computer and use it in GitHub Desktop.
- (void)rotatePath:(UIBezierPath *)path degree:(CGFloat)degree {
CGRect bounds = CGPathGetBoundingBox(path.CGPath);
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGFloat radians = (degree / 180.0f * M_PI);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, center.x, center.y);
transform = CGAffineTransformRotate(transform, radians);
transform = CGAffineTransformTranslate(transform, -center.x, -center.y);
[path applyTransform:transform];
}
@realNativLevy
Copy link

realNativLevy commented Feb 24, 2022

The swift version:

func rotatePath(path: UIBazierPath, angle: CGFloat) {
            let bounds = path.cgPath.boundingBox
            let center = CGPoint(x: bounds.midX, y: bounds.midY)
            
            var transform = CGAffineTransform.identity
            transform = transform.translatedBy(x: center.x, y: center.y)
            transform = transform.rotated(by: angle)
            transform = transform.translatedBy(x: -center.x, y: -center.y)
            path.apply(transform)
}

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