0

我在 Cappuccino 中的目标是调整 CPView 的框架,以便将绘制的矩形的每个点都包含在内。矩形是可旋转的,这使得它很棘手:( 来源:asawicki.info这里

我知道在 Cocoa 中我会使用 CGContextGetClipBoundingBox(context)。我将如何在卡布奇诺中处理这样的问题?

编辑:这就是我的问题的答案,基于亚历山大的提示:

var context = [[CPGraphicsContext currentContext] graphicsPort],
    transform = CGAffineTransformRotate(CGAffineTransformMakeTranslation(CGRectGetWidth([self bounds])/2, CGRectGetHeight([self bounds])/2), rotationRadians);

CGContextBeginPath(context);
path2 = CGPathCreateMutable();
CGPathAddRect(path2, transform, CGRectMake(-newWidth/2, -newHeight/2, newWidth, newHeight));
CGContextAddPath(context, path2);
CGContextClosePath(context);
CGContextSetFillColor(context, [CPColor yellowColor]);
CGContextFillPath(context);

CGContextRestoreGState(context);

var frame = [self frame];
frame.size.width = CGRectGetWidth([self bounds]);
frame.size.height =  CGRectGetHeight([self bounds]);
oldFrameWidth = frame.size.width;
oldFrameHeight = frame.size.height;
newFrameWidth = CGRectGetWidth(CGPathGetBoundingBox(path2));
newFrameHeight = CGRectGetHeight(CGPathGetBoundingBox(path2));
frame.size.width = newFrameWidth;
frame.size.height = newFrameHeight;
frame.origin.x -= (newFrameWidth - oldFrameWidth)/2;
frame.origin.y -= (newFrameHeight - oldFrameHeight)/2;
[self setFrame:frame];
4

1 回答 1

1

如果myPath是描述旋转矩形的 CGPath,则可以使用以下方法获取边界矩形:

CGPathGetBoundingBox(myPath)
于 2013-12-17T22:14:45.967 回答