核心图形旋转矩形

use*_*890 7 core-graphics rotation ios

通过这个公式,我得到了角度

double rotateAngle = atan2(y,x)
Run Code Online (Sandbox Code Playgroud)

使用此代码我可以绘制一个矩形

CGRect rect = CGRectMake(x,y , width ,height);
CGContextAddRect(context, rect);
CGContextStrokePath(context);
Run Code Online (Sandbox Code Playgroud)

如何围绕角度旋转矩形?

Ste*_*eve 27

这是你如何做到这一点:

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);
Run Code Online (Sandbox Code Playgroud)

  • 完善.mod应该将此标记为已接受,因为user1125890显然有更好的事情要做.我自己从未想过这一点.谢谢! (5认同)