如何使用DrawRect方法将Radius Corners设置为View?

时间:2015-08-28 11:17:15

标签: objective-c

使用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);

如何将半径角设置为特定视图。请帮帮我。

先谢谢。

1 个答案:

答案 0 :(得分:0)

您只需设置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];
}