貝塞爾曲線(UIBezierPath)屬性、方法匯總

原文鏈接:http://www.cnblogs.com/zhangying-domy/archive/2016/07/04/5640745.html

Quartz 2D 繪圖技術原文:http://www.lxweimin.com/p/020a81d625ee

//三角形

-(void)drawRect:(CGRect)rect

{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);

CGContextSetRGBFillColor(context, 0, 1, 0, 1);

CGContextSetLineWidth(context, 2.f);

CGContextSetLineJoin(context, kCGLineJoinRound);

CGContextSetLineCap(context, kCGLineCapRound);

CGPoint apoint[3];

apoint[0] = CGPointMake(100, 120);

apoint[1] = CGPointMake(150, 120);

apoint[2] = CGPointMake(150, 160);

CGContextAddLines(context, apoint, 3);

CGContextClosePath(context);

CGContextDrawPath(context, kCGPathFillStroke);

}

//矩形

-(void)drawRect:(CGRect)rect

{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);

CGContextSetRGBFillColor(context, 0, 1, 0, 1);

CGContextSetLineWidth(context, 2.f);

CGContextSetLineJoin(context, kCGLineJoinRound);

CGContextSetLineCap(context, kCGLineCapRound);

CGContextAddRect(context, CGRectMake(100, 120, 50, 50));

CGContextDrawPath(context, kCGPathFillStroke);

}

//圓

-(void)drawRect:(CGRect)rect

{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);

CGContextSetRGBFillColor(context, 0, 1, 0, 1);

CGContextSetLineWidth(context, 2.f);

CGContextAddEllipseInRect(context, CGRectMake(100, 120, 200, 100));

CGContextDrawPath(context, kCGPathFillStroke);

}

//圓弧

-(void)drawRect:(CGRect)rect

{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);

CGContextSetRGBFillColor(context, 0, 1, 0, 1);

CGContextSetLineWidth(context, 2.f);

CGContextSetLineCap(context, kCGLineCapRound);

// 繪制圓弧方法1 (參數由左至右分別是,圖形上下文、圓心x、圓心y、半徑、起始弧度、結束弧度、圓弧伸展的方向(0為順時針,1為逆時針))

CGContextAddArc(context, 150, 100, 100, arc(0), arc(160), 0);

CGContextClosePath(context);

CGContextDrawPath(context, kCGPathFillStroke);

}

//?文字

-(void)drawRect:(CGRect)rect

{

NSString * str = @"www.lxweimin.com";

NSMutableDictionary * attributes = [NSMutableDictionary dictionary];

attributes[NSFontAttributeName] = [UIFont systemFontOfSize:20];

attributes[NSBackgroundColorAttributeName] = [UIColor whiteColor];

attributes[NSForegroundColorAttributeName] = [UIColor redColor];

[str drawInRect:CGRectMake(100, 100, 200, 30) withAttributes:attributes];

}

//?圖片

-(void)drawRect:(CGRect)rect

{

UIImage * image = [UIImage imageNamed:@"123456.jpg"];

[image drawInRect:rect];

}

-(void)drawRect:(CGRect)rect

{

//直線

[[UIColor redColor] setStroke];

UIBezierPath * path = [UIBezierPath bezierPath];

path.lineWidth = 2.0;

path.lineCapStyle = kCGLineCapRound;

path.lineJoinStyle = kCGLineJoinRound;

[path moveToPoint:CGPointMake(10, 20)];

[path addLineToPoint:CGPointMake(50, 20)];

[path closePath];

[path stroke];

//三角形

[[UIColor redColor] setStroke];

UIBezierPath * path1 = [UIBezierPath bezierPath];

path1.lineWidth = 2.0;

path1.lineCapStyle = kCGLineCapRound;

path1.lineJoinStyle = kCGLineJoinRound;

[path1 moveToPoint:CGPointMake(10, 30)];

[path1 addLineToPoint:CGPointMake(50, 30)];

[path1 addLineToPoint:CGPointMake(30, 60)];

[path1 closePath];

[path1 stroke];

//長方形

[[UIColor redColor] setStroke];

UIBezierPath * path2 = [UIBezierPath bezierPathWithRect:CGRectMake(10, 80, 100, 50)];

[path2 stroke];

//圓弧

[[UIColor redColor] setStroke];

UIBezierPath * path3 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 100) radius:50 startAngle:arc(0) endAngle:arc(120) clockwise:YES];

[path3 stroke];

//圓

[[UIColor redColor] setStroke];

UIBezierPath * path4 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(200, 300, 100, 100) cornerRadius:50];

[path4 stroke];

[[UIColor redColor] setStroke];

UIBezierPath * path5 = [UIBezierPath bezierPath];

[path5 moveToPoint:CGPointMake(20, 200)];

[path5 addCurveToPoint:CGPointMake(120, 200) controlPoint1:CGPointMake(45, 150) controlPoint2:CGPointMake(95, 250)];

//? ? [path5 addQuadCurveToPoint:CGPointMake(120, 200) controlPoint:CGPointMake(45, 150)];

[path5 stroke];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容