1.画三角形 运行结果如下
![](https://img-blog.csdn.net/20150413201904075?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vubnlib3k5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
1.1具体实现步骤
1.1.1首先新建一个project,然后自定义一个view
1.2代码
- #import "htingShapeView.h"
-
- @implementation htingShapeView
-
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- }
- return self;
- }
-
- - (void)drawRect:(CGRect)rect
- {
- drawTriangle();
- }
-
- void draw4Rect()
- {
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
- CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));
-
-
-
-
- [[UIColor whiteColor] set];
-
-
-
-
- CGContextFillPath(ctx);
- }
-
- void drawTriangle()
- {
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
- CGContextMoveToPoint(ctx, 0, 0);
- CGContextAddLineToPoint(ctx, 100, 100);
- CGContextAddLineToPoint(ctx, 150, 80);
-
- CGContextClosePath(ctx);
-
-
- CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
-
-
- CGContextStrokePath(ctx);
- }
-
- @end
2.画矩形运行效果如下
![](https://img-blog.csdn.net/20150413202415714?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vubnlib3k5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.1具体实现步骤
2.1.1搭建界面同上
2.1.2代码
- #import "htingShapeView.h"
-
- @implementation htingShapeView
-
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- }
- return self;
- }
-
- - (void)drawRect:(CGRect)rect
- {
- draw4Rect();
- }
-
- void draw4Rect()
- {
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
- CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));
-
-
-
-
- [[UIColor whiteColor] set];
-
-
-
-
- CGContextFillPath(ctx);
- }
-
- void drawTriangle()
- {
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
- CGContextMoveToPoint(ctx, 0, 0);
- CGContextAddLineToPoint(ctx, 100, 100);
- CGContextAddLineToPoint(ctx, 150, 80);
-
- CGContextClosePath(ctx);
-
-
- CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
-
-
- CGContextStrokePath(ctx);
- }
-
- @end
3.画圆 圆弧 等 运行效果如下
3.1代码实现
- #import "htingCircleView.h"
-
- @implementation htingCircleView
-
-
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- }
- return self;
- }
-
- - (void)drawRect:(CGRect)rect
- {
- drawCircle();
- }
-
- void drawCircle2()
- {
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
- CGContextMoveToPoint(ctx, 100, 100);
- CGContextAddLineToPoint(ctx, 100, 150);
- CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI, 1);
- CGContextClosePath(ctx);
-
- [[UIColor redColor] set];
-
-
- CGContextFillPath(ctx);
-
- }
-
-
- void drawArc()
- {
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
-
-
-
-
-
-
- CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
-
-
-
-
- CGContextFillPath(ctx);
-
- }
-
- void drawCircle()
- {
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
- CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 100, 100));
-
- CGContextSetLineWidth(ctx, 10);
-
-
- CGContextStrokePath(ctx);
- }
-
- @end
转载于:https://www.cnblogs.com/Free-Thinker/p/5948737.html