簡體   English   中英

設置CPView框架,使其包圍繪制矩形

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

我在卡布奇諾咖啡店的目標是調整CPView的框架,以便將繪制的矩形的每個點都包住。 rect是可旋轉的,這使它變得棘手: 這里
(來源: asawicki.info

我知道在可可中我會使用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];

如果myPath是描述旋轉矩形的CGPath,則可以使用以下方法獲取邊界矩形:

CGPathGetBoundingBox(myPath)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM