【问题标题】:How to set the Radius Corners to View using DrawRect Method?如何使用 DrawRect 方法将半径角设置为查看?
【发布时间】:2015-08-28 11:17:14
【问题描述】:

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

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

提前致谢。

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    您可以简单地设置UIView 的圆角半径:

      view.layer.cornerRadius = 5;
    

    如果您使用的是 beizer path ,您可以使用以下代码设置圆角半径:

    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 创建它?
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多