Ios 核心图形旋转矩形

Ios 核心图形旋转矩形,ios,rotation,core-graphics,Ios,Rotation,Core Graphics,用这个公式,我得到了角度 double rotateAngle = atan2(y,x) 用这段代码我可以画一个矩形 CGRect rect = CGRectMake(x,y , width ,height); CGContextAddRect(context, rect); CGContextStrokePath(context); 如何围绕该角度旋转矩形?以下是您的操作方法: CGContextSaveGState(context); CGFloat halfWidth = width

用这个公式,我得到了角度

double rotateAngle = atan2(y,x)
用这段代码我可以画一个矩形

CGRect rect = CGRectMake(x,y , width ,height);
CGContextAddRect(context, rect);
CGContextStrokePath(context);

如何围绕该角度旋转矩形?

以下是您的操作方法:

CGContextSaveGState(context);

CGFloat halfWidth = width / 2.0;
CGFloat halfHeight = height / 2.0;
CGPoint center = CGPointMake(x + halfWidth, y + halfHeight);

// Move to the center of the rectangle:
CGContextTranslateCTM(context, center.x, center.y);
// Rotate:
CGContextRotateCTM(context, rotateAngle);
// Draw the rectangle centered about the center:
CGRect rect = CGRectMake(-halfWidth, -halfHeight, width, height);
CGContextAddRect(context, rect);
CGContextStrokePath(context);

CGContextRestoreGState(context);

完美的国防部应该将此标记为已接受,因为用户1125890显然有更好的事情要做。我自己永远也不会明白这一点。非常感谢。你救了我的命,兄弟。我试图根据superview标签的大小旋转我的直肠。。从最近几天开始变得越来越黑暗。但你的解决方案很有魅力。谢谢。