Affine转换CGPoint

时间:2018-03-10 22:49:21

标签: ios objective-c cgpoint affinetransform

我整个上午都在做研究。到目前为止没有运气。我试图围绕视图中心旋转CGPoint。

这是我为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);

它的工作很棒。我发现这篇文章What is the best way to rotate a CGPoint on a grid?并且它只是没有用。

我真的需要一种方法来围绕我的边界旋转CGPoint。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您计算转换的代码是正确的。您需要做的就是将转换应用到这一点。

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