绘制图形代码

画线

- (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];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容