ios – 核心图形旋转矩形

通过这个公式我得到了角度
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,rect);
CGContextstrokePath(context);

CGContextRestoreGState(context);

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...