我们来画弧线,我们需要达到的目标是如图所示
可以用CGContext 去实现
[objc]view plaincopy
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx,1,0,0,1);//设置线的颜色
CGContextSetRGBFillColor(ctx,0,0,1,1);//设置填充颜色
CGContextSetLineWidth(ctx,2);//设置线的宽度
CGContextAddEllipseInRect(ctx, CGRectMake(10,30,60,60));//画一个椭圆或者圆
CGContextDrawPath(ctx, kCGPathFillStroke);
//根据中心点,半径,起始的弧度,最后的弧度,是否顺时针画一个圆弧
CGContextAddArc(ctx,140,60,30, M_PI/2.f, M_PI,1);
CGContextDrawPath(ctx, kCGPathStroke);
CGPoint p[3] =
{
CGPointMake(210.0,30.0),
CGPointMake(210.0,60.0),
CGPointMake(240.0,60.0),
};
//先移到p1点
CGContextMoveToPoint(ctx, p[0].x, p[0].y);
//从p1点开始画弧线,圆弧和p1-p2相切;p2-p3和弧线相切,圆弧的半径是20
CGContextAddArcToPoint(ctx, p[1].x, p[1].y, p[2].x, p[2].y,20.0);
CGContextStrokePath(ctx);
//画一个圆角矩形
CGRect rrect = CGRectMake(210.0,70.0,60.0,60.0);
CGFloat radius =10.0;
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
CGContextMoveToPoint(ctx, minx, midy);
CGContextAddArcToPoint(ctx, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, radius);
CGContextClosePath(ctx);
CGContextDrawPath(ctx, kCGPathFillStroke);
//贝塞尔曲线一,两个控制点
CGPoint s = CGPointMake(30.0,120.0);//起始点
CGPoint e = CGPointMake(300.0,120.0);//终点
CGPoint cp1= CGPointMake(120.0,30.0);//控制点1
CGPoint cp2= CGPointMake(210.0,210.0);//控制点2
CGContextMoveToPoint(ctx, s.x, s.y);
CGContextAddCurveToPoint(ctx, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
CGContextStrokePath(ctx);
//贝塞尔曲线二,一个控制点
s = CGPointMake(30.0,300.0);
e = CGPointMake(270.0,300.0);
cp1= CGPointMake(150.0,180.0);
CGContextMoveToPoint(ctx, s.x, s.y);
CGContextAddQuadCurveToPoint(ctx, cp1.x, cp1.y, e.x, e.y);
CGContextStrokePath(ctx);
用UIKit去实现
[objc]view plaincopy
UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(10,30,60,60)];
[[UIColorredColor]setStroke];
[[UIColorblueColor]setFill];
path.lineWidth=2;
[pathstroke];
[pathfill];
path = [UIBezierPathbezierPathWithArcCenter:CGPointMake(140,60)radius:30startAngle:M_PI/2.fendAngle:M_PIclockwise:YES];
path.lineWidth=2;
[pathstroke];
path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(210,70,60,60)cornerRadius:5];
path.lineWidth=2;
[pathstroke];
[pathfill];
path = [UIBezierPathbezierPath];
path.lineWidth=2;
CGPoint s = CGPointMake(30.0,120.0);
CGPoint e = CGPointMake(300.0,120.0);
CGPoint cp1= CGPointMake(120.0,30.0);
CGPoint cp2= CGPointMake(210.0,210.0);
[pathmoveToPoint:s];
[pathaddCurveToPoint:econtrolPoint1:cp1controlPoint2:cp2];
[pathstroke];
path = [UIBezierPathbezierPath];
path.lineWidth=2;
s = CGPointMake(30.0,300.0);
e = CGPointMake(270.0,300.0);
cp1= CGPointMake(150.0,180.0);
[pathmoveToPoint:s];
[pathaddQuadCurveToPoint:econtrolPoint:cp1];
[pathstroke];