在Paintcode 2中,我在画布内的框架内有一个圆圈。
圆圈的约束设置如下:
为了让圆圈变大并且不变成椭圆,我必须
这种类型的代码有什么办法吗?
-(void)drawRect:(CGRect)rect {
if (rect.size.width > rect.size.height) {
rect.origin.x = (rect.size.width - rect.size.height) * .5f;
rect.size.width = rect.size.height;
} else {
rect.origin.y = (rect.size.height - rect.size.width) * .5f;
rect.size.height = rect.size.width;
}
NSLog(@"Frame=%@", NSStringFromCGRect(rect));
[CircleDraw drawCircleWithFrame:rect];
}
答案 0 :(得分:4)
150x120
10, 10, 100, 100
frame
的新变量,输入矩形:10, 10, 150, 100
min(frame.height, frame.width)
position
(下方)smallestSide
拖动到椭圆的高度和宽度position
拖动到椭圆的位置<小时/> 位置(表达式)
makePoint(
frame.x+(frame.width-smallestSide)*0.5,
frame.y+(frame.height-smallestSide)*0.5
)
<小时/> 的输出强>
- (void)drawCanvas1WithFrame: (CGRect)frame
{
//// Variable Declarations
CGFloat smallestSide = MIN(frame.size.height, frame.size.width);
CGPoint position = CGPointMake(frame.origin.x + (frame.size.width - smallestSide) * 0.5, frame.origin.y + (frame.size.height - smallestSide) * 0.5);
//// Oval Drawing
UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(position.x, position.y, smallestSide, smallestSide)];
[UIColor.grayColor setFill];
[ovalPath fill];
}
注意:我在PaintCode的Matt Dunik帮助下解决了这个问题,但解决方案实际上非常简单。