iOS 画直线,虚线,圆,矩形
画矩形
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [_axisColor CGColor]);
CGContextSetLineWidth(ctx, 1.0);
CGPoint poins[] = {CGPointMake(_margin, _margin),CGPointMake(_margin+_axisWidth, _margin),CGPointMake(_margin+_axisWidth, _margin+_axisHeight),CGPointMake(_margin, _margin+_axisHeight)};
CGContextAddLines(ctx,poins,4);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
画虚线
CGPoint aPoints[2];//坐标点
aPoints[0] =CGPointMake(_margin, _margin+i*(_axisHeight/_gridStep));//坐标1
aPoints[1] =CGPointMake(_margin+_axisWidth, _margin+i*(_axisHeight/_gridStep));//坐标2
CGContextSetStrokeColorWithColor(ctx, _axisColor.CGColor);
CGFloat lengths[2] = {5,5};
CGContextSetLineDash(ctx, 0, lengths, 1); //画虚线
CGContextMoveToPoint(ctx, aPoints[0].x,aPoints[0].y); //开始画线
CGContextAddLineToPoint(ctx, aPoints[1].x,aPoints[1].y);
CGContextStrokePath(ctx);
画圆
UIColor * color = [_colorArray objectAtIndex:_index];
CGFloat rColor,gColor,bColor,alph;
[color getRed:&rColor green:&gColor blue:&bColor alpha:&alph];
/*画圆*/
CGContextSetRGBStrokeColor(context,rColor,gColor,bColor,1);//画笔线的颜色
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGFloat lengths[2] = {5,5};
CGContextSetLineDash(context, 0, lengths, 0);//防止被虚线
CGContextSetLineWidth(context, 2.0);//线的宽度
CGFloat radius = 3;//半径
CGContextAddArc(context, point.x, point.y,radius, 0, 2*M_PI, 0); //添加一个圆
CGContextDrawPath(context, kCGPathFillStroke); //绘制路径//kCGPathStroke//kCGPathFill
评论