0
我的目标是在卡布奇诺调整CPView的框架,以便绘制矩形的每个点都被包住。矩形是可旋转的,这使得它很棘手: here http://www.asawicki.info/files/aabb_of_rotated_rectangle.png。设置CPView框架,使其包围绘制矩形
我明白,在可可我会用CGContextGetClipBoundingBox(上下文)。 我将如何处理卡布奇诺的这种问题?
编辑:这是回答我的问题的基础上,亚历山大的提示:
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];
Alex,我目前有正确的边界CPView的尺寸(请参阅上面的代码),但绘制的矩形的中心位于CPView的左上角。我怎样才能把它带到CPView的中心? – saeppi
那是因为你正在用这种方式绘制你的矩形,在(-newWidth/2,newHeight/2)。如果要在中心绘制它,则可以将转换添加到边界宽度和高度的一半的变换中。 –
谢谢。如何将翻译添加到变换而不覆盖旋转? – saeppi