如何使用DrawRect方法设置要查看的半径角?
问题描述:
使用DrawRect方法绘制一个矩形视图。我必须设置该特定矩形视图的半径拐角。我写下了一个矩形视图。如何使用DrawRect方法设置要查看的半径角?
- (void)drawRect:(CGRect)rect {
int coordinate_x=60,coordinate_y=120,width=200,height=295;
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect drawTank = {coordinate_x, coordinate_y, width, height};
CGColorRef blackColor=[UIColor blackColor].CGColor;
CGContextSetRGBStrokeColor(context, 0, 0, 255, 1);
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, blackColor);
CGContextStrokeRect(context, drawTank);
}
而我必须使用下面的填充视图和空视图使用代码。
CGRect emptyTank = CGRectMake(coordinate_x, coordinate_y, width, height *(1-filledPercentage));
CGRect fullTank = CGRectMake(coordinate_x, coordinate_y+height *(1-filledPercentage),width, height * filledPercentage);
如何设置特定视图的半径拐角。请帮帮我。
在此先感谢。
答
,你可以简单地设置一个UIView
与圆角半径:
view.layer.cornerRadius = 5;
如果使用beizer路径,你可以设置角半径用下面的代码:
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(55, 30, 133, 51) cornerRadius: 5];
[UIColor.grayColor setFill];
[rectanglePath fill];
中的drawRect内方法:
- (void)drawRect: (CGRect)frame
{
//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(CGRectGetMinX(frame) + 40, CGRectGetMinY(frame) + 21, 133, 51) cornerRadius: 7];
[UIColor.grayColor setFill];
[rectanglePath fill];
}
请发送完整的代码来绘制半径角的视图。 –
我的答案有代码,它有两种方法可以做到这一点。你喜欢哪一个?以编程方式创建uiview?或从sotryboard创建它? –