关于CGContextRef画图

来源:互联网 发布:arm c语言编程实例 编辑:程序博客网 时间:2024/05/29 17:39


 1.此处方法是得到一个矩形

-(void)drawRect:(CGRect)rect

{

    //获得当前画板

    CGContextRefcontext=UIGraphicsGetCurrentContext();

    //设置线条的颜色

    CGContextSetRGBStrokeColor(context,0.2,0.2,0.2,1.0);

    //设置线条的宽度

    CGContextSetLineWidth(context,1);

    //设置所选取的区域

    CGContextAddRect(context,CGRectMake(1,1,self.bounds.size.width-1,self.bounds.size.height-1));

    //开始画线

    CGContextStrokePath(context);

    [super drawRect:rect];

}

2.此处方法得到一条线

-(void)drawRect:(CGRect)rect{

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context,[UIColorcolorWithRed:130/255.0green:130/255.0blue:130/255.0alpha:1.0].CGColor);

    CGContextFillRect(context,CGRectMake(0,self.bounds.size.height-0.8,self.bounds.size.width,0.8));

}


3.此处方法得到一个圆形区域

-(void)drawRect:(CGRect)rect

{

    CGContextRefcontext=UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context,0.2,0.2,0.2,1.0);

    CGContextSetLineWidth(context,1);

   CGContextAddArc(context,self.bounds.size.width/2,self.bounds.size.height/2,self.bounds.size.width/2,0,M_PI*2,0);

    CGContextDrawPath(context,kCGPathStroke);

    self.clipsToBounds =YES;

    self.layer.cornerRadius =self.bounds.size.width/2;

    [super drawRect:rect];

}


0 0
原创粉丝点击