简体   繁体   English

设置CPView框架,使其包围绘制矩形

[英]Setting CPView frame so that it encases drawn rect

my goal in Cappuccino is to adjust the CPView's frame so that every point of a drawn rect is encased. 我在卡布奇诺咖啡店的目标是调整CPView的框架,以便将绘制的矩形的每个点都包住。 The rect is rotatable which makes it tricky: rect是可旋转的,这使它变得棘手: 这里
(source: asawicki.info ) (来源: asawicki.info
.

I understand that in Cocoa I'd use CGContextGetClipBoundingBox(context). 我知道在可可中我会使用CGContextGetClipBoundingBox(context)。 How would I handle such a problem in Cappuccino? 我将如何处理卡布奇诺咖啡中的此类问题?

Edit : That's the answer to my problem, based on Alexander's tips: 编辑 :根据亚历山大的提示,这就是我的问题的答案:

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];

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

CGPathGetBoundingBox(myPath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM