iOS中绘制圆形的函数方法

- (void)drawRect:(CGrect)rect

{

    CGRect bounds = self.bounds;//根据bounds计算中心点

    

    CGPoint center;  //圆心定位屏幕的中心

    center.x = bounds.origin.x + bounds.size.width / 2.0;  //分别计算屏幕的长河宽,进行比较,因为绘制的圆形不应该超出屏幕的界限,以较短的那个的一半作为半径

    center.y = bounds.origin.y + bounds.size.height / 2.0;

    

    float radius = (MIN(bounds.size.height, bounds.size.width) / 2.0);

    

    UIBezierPath *path = [[UIBezierPath alloc]init];//使用UIBezierPath类绘制圆形,相关用法可以查阅api文档,可以记住

    

    [path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];//以中心点为圆心,radius为半径,定义一个0-M_PI * 2.0弧度的路径,即为整圆,顺时针可以选YES或NO,因为是整圆为无所谓

    

    //设置线条宽度为10

    path.lineWidth = 10;

    

    //设置绘制颜色

    [[UIColor lightGrayColor] setStroke];

    

    //绘制路径

    [path stroke];

}

 如果在.m文件中打印该方法是灰出现叹号,显示bounds没有定义,是因为在.h文件的继承上出现了问题。

解决方啊如下:

     新建文件的的时候选择Cocoa Touch->Object-C class,点击next,其中Subclass of要选择UIView这样就可以解决了。

原文地址:https://www.cnblogs.com/lepus/p/4767102.html