最近有一些IOS问了我有关Quart2D的问题,以下就简单的来展示一下,因为博客写的不多,写得不好别见怪,以下的内容都比较简单,适合新手看
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.生成一张以后用于平铺的小图片
CGSize size = CGSizeMake(self.view.frame.size.width, 35);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
//2.画矩形
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat height = 35;
CGContextAddRect(ctx, CGRectMake(0, 0, self.view.frame.size.width, height));
[[UIColor whiteColor]setFill];
CGContextFillPath(ctx);
//3.画线条
CGFloat lineWidth = 2;
CGFloat lineY = height-lineWidth;
CGFloat lineX = 0;
CGContextMoveToPoint(ctx, lineX, lineY);
CGContextAddLineToPoint(ctx, self.view.frame.size.width, lineY);
[[UIColor blackColor] setStroke];
CGContextStrokePath(ctx);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIColor *color=[UIColor colorWithPatternImage:image];
self.view.backgroundColor=color;
// self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
下面的就是利用drawRect来画出各种图形
-(void)layoutSubviews
{
self.backgroundColor = [UIColor whiteColor];
}
- (void)drawRect:(CGRect)rect {
#pragma -mark 直线
/*
self.backgroundColor = [UIColor whiteColor];
//取得当前视图相关联的图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置起点
CGContextMoveToPoint(ctx, 20, 100);
//设置终点
CGContextAddLineToPoint(ctx, 300, 100);
//设置绘图形状
//设置线条颜色
CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);
//设置线条宽度
CGContextSetLineWidth(ctx, 10);
//设置线条起点和终点的样式为圆角
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置线条的转角的样式为圆角
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//3.渲染(绘制出一条空心的线)
CGContextStrokePath(ctx);
// //注意线条不能渲染为实心的
// CGContextFillPath(ctx);
*/
#pragma -mark 三角形
/*
//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//绘制三角形
//设置起点
CGContextMoveToPoint(ctx, 20, 100);
//设置第二个点
CGContextAddLineToPoint(ctx, 40, 300);
//设置第三个点
CGContextAddLineToPoint(ctx, 200, 100);
//设置终点
// CGContextAddLineToPoint(ctx, 20, 100);
//关闭起点和终点
CGContextClosePath(ctx);
//3.渲染到layer上
CGContextStrokePath(ctx);
*/
#pragma -mark 四边形
/*
//1.获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.画四边形
CGContextAddRect(ctx, CGRectMake(20, 20, 150, 100));
//如果要设置绘图的状态必须在渲染之前
// CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
//绘制什么类型的图形(空心或者实心),就要通过什么类型的方法设置状态
CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
//调用OC的方法设置绘图的颜色
// [[UIColor purpleColor] setFill];
// [[UIColor blueColor] setStroke];
//3.渲染图形到layer上
//空心的
// CGContextStrokePath(ctx);
//实心的
CGContextFillPath(ctx);
*/
#pragma -mark 画圆
/*
//圆1
// //1.获取上下文
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// //画圆
// CGContextAddArc(ctx, 100, 100, 54, 0, 2*M_PI, 0);
// //3.渲染(注意,画线只能通过空心来画)
// CGContextStrokePath(ctx);
//圆2
获取上下文
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// //画圆
// CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 50, 50));
// [[UIColor greenColor] setFill];
// //渲染
// CGContextFillPath(ctx);
//画椭圆3
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, CGRectMake(150, 200, 100, 200));
[[UIColor yellowColor] setFill];
CGContextFillPath(ctx);
*/
//画圆弧
/*
//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.画圆弧
//x/y 圆心
//radius 半径
//startAngle 开始的弧度
//endAngle 结束的弧度
//clockwise 画圆弧的方向(0 顺时针,1 逆时针)
// CGContextAddArc(ctx, 150, 100, 20, -M_PI_2, M_PI_2, 1);
CGContextAddArc(ctx, 150, 100, 20, M_PI, 0, 0);
// CGContextClosePath(ctx);
// CGContextFillPath(ctx);
// CGContextAddArcToPoint(ctx, 200, 140, 200, 200, 50);
CGContextAddArc(ctx, 190, 100, 20, M_PI, 0, 1);
CGContextAddArc(ctx, 230, 100, 20, M_PI, 0, 0);
CGContextAddArc(ctx, 270, 100, 20, M_PI, 0, 1);
CGContextAddArc(ctx, 310, 100, 20, M_PI, 0, 0);
// CGPathAddArc(ctx, <#const CGAffineTransform * _Nullable m#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#bool clockwise#>)
CGContextStrokePath(ctx);
*/
#pragma -mark 扇形图
//画饼状图
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// //画线
// CGContextMoveToPoint(ctx, 100, 100);
// CGContextAddLineToPoint(ctx, 100, 150);
// //画圆弧
// CGContextAddArc(ctx, 100, 100, 30, M_PI_2, M_PI, 0);
// [[UIColor brownColor] setFill];
// //关闭路径
// CGContextClosePath(ctx);
// //渲染
// CGContextFillPath(ctx);
#pragma -mark 画文字
/*
NSString *str = @"View内部有个layer(图层)属性,drawRect:方法中取得的是一个Layer Graphics Context,因此,绘制的东西其实是绘制到view的layer上去了";
//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.绘图
//绘制矩形
CGContextAddRect(ctx, CGRectMake(100, 200, 100, 200));
//3.渲染
CGContextStrokePath(ctx);
NSMutableDictionary *md = [NSMutableDictionary dictionary];
//设置文字颜色
md[NSForegroundColorAttributeName] = [UIColor redColor];
//设置文字背景颜色
md[NSBackgroundColorAttributeName] = [UIColor greenColor];
//设置文字大小
md[NSFontAttributeName] = [UIFont systemFontOfSize:15];
//将文字绘制到指定的位置
// [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];
//将文字指定到指定的范围内, 如果一行装不下会自动换行,当文字超出范围后就不显示
[str drawInRect:CGRectMake(100, 200, 100, 200) withAttributes:md];
*/
#pragma -mark 图片
/*
//平铺效果
//加载图片到内存中
UIImage *image = [UIImage imageNamed:@"iconfont-shoucang"];
//利用drawAsPatterInRect方法绘制图片到layer ,是通过平铺原有的图片
[image drawAsPatternInRect:CGRectMake(0, 0, 375, 667)];
*/
/*
//拉伸效果
UIImage *image = [UIImage imageNamed:@"iconfont-shoucang"];
//利用drawInRect方法绘制图片到layer, 是通过平铺原有图片
[image drawInRect:CGRectMake(0, 0, 200, 200)];
*/
/*
//把图片固定在一个位置
UIImage *image = [UIImage imageNamed:@"iconfont-shoucang"];
//将图片绘制到指定的位置
[image drawAtPoint:CGPointMake(100, 100)];
*/
}