Objective-c:如何旋转 CGRect

标签 objective-c macos cgrect cgpoint

我尝试旋转我的矩形,但它不能正常工作。

这是我的部分代码,我在另一篇文章中找到的:

#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0)
-(void)drawRect:(NSRect)rect
{
   CGContextRef cRef = [[NSGraphicsContext currentContext] graphicsPort];

   // points[] - this is a CGPoints array, length_one is a double value
   CGRect rect_one = CGRectMake(points[0].x, points[0].y, (CGFloat)length_one, 40);

   // I've print out the origin of old one and new one
   NSLog(@"old rect -- %f, %f", rect_one.origin.x, rect_one.origin.y);

  float centerX = rect_one.origin.x + (rect_one.size.width / 2.0);
  float centerY = rect_one.origin.y + (rect_one.size.height / 2.0);

  CGAffineTransform rotation = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(10));
  CGAffineTransform moveAnchor = CGAffineTransformMakeTranslation(centerX, centerY);

  CGAffineTransform centeredRotation = CGAffineTransformConcat(moveAnchor, rotation);

  CGRect rotatedRect = CGRectApplyAffineTransform(rect_one, centeredRotation);
  CGContextAddRect(cRef, rotatedRect);

  // new one
  NSLog(@"new rect -- %f, %f", rotatedRect.origin.x, rotatedRect.origin.y);
}

即使我无法从 View 中找到我的新矩形,原点也发生了很大变化。 旧原点是 (x = 263.3, y = 502.8),新原点是 (x=506.1, y=1132.0) 这个系统是如何工作的,特别是如何分配旋转角度?如果可能的话,你们能帮我简单解释一下吗?非常感谢!!!!

最佳答案

正如其他人所说,NSRectCGRect 始终是轴对齐 矩形。 CGRectApplyAffineTransform 返回平移矩形的轴对齐边界框:

diagram of rectangles

请注意,如果您应用了旋转,新矩形(边界框)的尺寸可能与原始矩形不同。这意味着如果要绘制变换后的矩形,则对矩形应用变换是没有用的。

相反,您需要转换图形上下文的坐标系。可以这样想:您正在 table 上的一张纸上作画。您总是绘制一个与 table 边缘对齐的矩形。要绘制一个旋转的矩形,您旋转纸张,然后像往常一样绘制一个矩形。更改 CTM 就像旋转(或移动、缩小或扩大)纸张。

-(void)drawRect:(NSRect)rect {
    CGContextRef gc = [[NSGraphicsContext currentContext] graphicsPort];

    // points[] - this is a CGPoints array, length_one is a double value
    CGRect rect = CGRectMake(points[0].x, points[0].y, (CGFloat)length_one, 40);

    CGFloat xMid = CGRectGetMidX(rect);
    CGFloat yMid = CGRectGetMidY(rect);

    CGContextSaveGState(gc); {
        // Translate the origin to the midpoint of the rectangle, because rotations
        // always happen around the origin.
        CGContextTranslateCTM(gc, xMid, yMid);

        // Rotate the coordinate system by 10 degrees.
        CGContextRotateCTM(gc, 10 * M_PI / 180);

        // Prepare a new rectangle, with the same size as the original rectangle
        // but centered on the origin.
        CGRect newRect = rect;
        newRect.origin.x = -newRect.size.width / 2;
        newRect.origin.y = -newRect.size.height / 2;

        // Add the rectangle to the context's path.
        CGContextAddRect(gc, newRect);

        // Draw the new rectangle.
        CGContextSetFillColorWithColor(gc, [NSColor lightGrayColor].CGColor);
        CGContextSetStrokeColorWithColor(gc, [NSColor blackColor].CGColor);
        CGContextSetLineWidth(gc, 2);
        CGContextSetLineJoin(gc, kCGLineJoinMiter);
        CGContextDrawPath(gc, kCGPathFillStroke);
    } CGContextRestoreGState(gc);

}

关于Objective-c:如何旋转 CGRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710933/

相关文章:

ios - 将两个 CGPoints 变成一个 CGRect

错误按钮上的 iOS 标题

macos - 从无代码 KEXT 迁移到 DEXT 后的性能问题

macos - HIDictionaryWindowShow 在 Swift 中的用法

text - UITextField - 多行

objective-c - 为什么两个 Integer 不相等?

ios - 类中定义的方法无法识别

objective-c - 将 objective-c block 转换为 Swift 闭包

ios - JASidepanels 不显示导航按钮

python - Django with mysql安装模块macos