1

I've been doing research all morning. No luck so far. I'm trying to rotate a CGPoint around the center of my view.

Here's what I do for UIBezierPath:

CGRect bounds = self.bounds; // might want to use CGPathGetPathBoundingBox
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, center.x, center.y);
transform = CGAffineTransformRotate(transform, DEGREES_TO_RADIANS(degreesOfRotation));
transform = CGAffineTransformTranslate(transform, -center.x, -center.y);
currentLayer.path = CGPathCreateCopyByTransformingPath(pathTwo.CGPath, &transform);

It work's great. I found this post What is the best way to rotate a CGPoint on a grid? and it just didn't work.

I really need a way to rotate CGPoint around the center of my bounds.

Any ideas?

digit
  • 197
  • 9

1 Answers1

0

Your code to calculate the transform is correct. All you need to do is apply the transform to the point.

CGRect bounds = self.bounds;
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, center.x, center.y);
transform = CGAffineTransformRotate(transform, DEGREES_TO_RADIANS(degreesOfRotation));
transform = CGAffineTransformTranslate(transform, -center.x, -center.y);

CGPoint point = CGPointMake(bounds.size.width - 10, center.y);  // some point
CGPoint point2 = CGPointApplyAffineTransform(point, transform); // some point rotated about the center of the view
user3386109
  • 34,287
  • 7
  • 49
  • 68