绘制图形代码

画线

- (void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(context, 10, 10);

CGContextAddLineToPoint(context, 30, 100);

CGContextStrokePath(context);

}

画三角形

- (void)drawRect:(CGRect)rect{

//画三角形

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);

CGContextMoveToPoint(context, 10, 10);

CGContextAddLineToPoint(context, 110, 10);

CGContextAddLineToPoint(context, 110, 110);

CGContextClosePath(context);

CGContextStrokePath(context);

}

画矩形

- (void)drawRect:(CGRect)rect{

//画矩形

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddRect(context, CGRectMake(10, 20, 100, 100));

//CGContextFillPath(context);

CGContextStrokePath(context);

}

画扇形

- (void)drawRect:(CGRect)rect{

//画扇形

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(context, 100, 100);

CGContextAddArc(context, 100, 100,60, - 3 * M_PI_4, -M_PI_4, 1);

CGContextClosePath(context);

CGContextStrokePath(context);

}

画弧

-(void)drawArc{

CGContextRef context = UIGraphicsGetCurrentContext();

//x,y 圆心

//radius 半径

//startAngle 画弧的起始位置

//endAngel 画弧的结束位置

//clockwise 0 顺针 1 逆时针

CGContextAddArc(context, 100, 100, 60, 0, M_PI, 1);

CGContextClosePath(context);

//渲染

CGContextStrokePath(context);

//CGContextFillPath(context);

}

画圆

- (void)drawRect:(CGRect)rect{

//画圆

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddEllipseInRect(context, CGRectMake(10, 10, 100, 100));

CGContextStrokePath(context);

}

画图片

- (void)drawRect:(CGRect)rect{

//画图片

UIImage *image = [UIImage imageNamed:@"papa"];

[image drawAsPatternInRect:CGRectMake(10, 10, 50, 50)];

}

画文字

- (void)drawRect:(CGRect)rect{

//画文字

NSString *str = @"啦啦啦啦啦啦啦啦啦啦啦啦";

NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor yellowColor]};

[str drawInRect:CGRectMake(10, 10, 100, 100) withAttributes:attr];

}

你可能感兴趣的:(绘制图形代码)