Objective-c:如何旋转CGRect

时间:2013-09-10 04:51:06

标签: 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);
}

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

2 个答案:

答案 0 :(得分:6)

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

diagram of rectangles

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

相反,您需要转换图形上下文的坐标系。可以这样想:你正在桌子上画一张纸。您始终绘制一个与桌面边缘对齐的矩形。要绘制旋转的矩形,请旋转纸张,然后照常绘制矩形。更改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);

}

答案 1 :(得分:0)

override func drawRect(rect: CGRect) {
    super.drawRect(rect)
    let context : CGContextRef = UIGraphicsGetCurrentContext()
    var rect = CGRectMake(100, 100, 120, 180)
    CGContextSaveGState(context);
    var path :CGMutablePathRef  = CGPathCreateMutable();
    var midX : CGFloat = CGRectGetMidX(rect);
    var midY : CGFloat = CGRectGetMidY(rect);
    var transfrom: CGAffineTransform  =
    CGAffineTransformConcat(
        CGAffineTransformConcat(CGAffineTransformMakeTranslation(-midX, -midY),CGAffineTransformMakeRotation(0.3)),
        CGAffineTransformMakeTranslation(midX, midY));
    CGPathAddRect(path, &transfrom, rect);
    CGContextSetFillColorWithColor(context, UIColor.blueColor().CGColor);
    CGContextAddPath(context, path);
    CGContextFillPath(context);
    CGContextRestoreGState(context);
}