iOS复习记录日记13-绘图[2020]

前文

主要记录 ios的绘图知识点,很麻烦绘图,对于我几乎用不上

正文

补充Modal跳转方式
Modal是从下往上跳转的,push是从右向左跳转的
使用Modal在业务逻辑上是无关系的填表类型.
[self persentViewController:uiview animated:YES 回调]//跳转
[self dismissViewControllerAnimated:YES 回调]//完成
如果要在main:sb里面设置线模式即可.

绘图

quartz2d是一个二维绘图引擎,支持iOS和macosx,纯C语言的
它包含在CoreGraphics框架中
它可以 绘制图形/线条/三角形/矩形/圆/弧
绘制文字/绘制/生成图片/读取/生成PDF
裁剪图片/自定义UI控件

1.获取图形上下文对象 (相当于草稿纸)
图形上下文类型:CGContextRef
2.向图形上下文对象中添加路径 (路径相当于笔线)
3.渲染 (把草稿纸画好的,按照草稿纸上的对目标花)


在UIview里面的-(void)drawRect中绘画,这是渲染前的重写函数
1.获得上下文(草稿纸)
CGContextRef ctx = UIGraphicsGetCurrentContext();
2.添加拼接路径 同时添加到上下文中
CGContextMoveToPoints(上下文,起点x,起点y);
CGContextAddLineToPoint(上下文,终点x,终点y);
3.渲染
CGContextStrokePath(上下文);

C语言的方式1种:
CGContextRef ctx = CGGraphicsGetCurrentContext();
CGContextMoveToPoints();
CGContextADdLineToPoints();
GGContextStrokePath();
C语言的方式2种:
CGContextRef ctx = CGGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();//获得可变的路径存储对象
CGPathMoveToPoints(path,旋转平移,起点x,起点y)//给可变路径存储对象添加
CGPathAddLineToPoint(path,旋转平移,终点x,终点y);//给可变路径添加路径
CGContextaddPath(ctx,path);// 讲 可变路径对象的数组 添加给 上下文
CGContetStrokePath();//上下文渲染
C+OC语言的方式3种:
CGContextRef ctx = CGGraphicsGetCurrentContext()
UIBezierPath *path = [UIBezierPath alloc]init];//oc路径对象
[path moveToPoint:CGPointsMake(50,50);]
[path addLineToPoint:CGPointsMake(10,10);]
CGContextaddPath(ctx,path.CGPath);//把oc的path转成c的path
CGContextStrokePath(ctx);
C+OC语言的方式4种:
CGContextRef ctx = CGGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoints(path,xxx,xxx,xxx);
CGPathAddLineToPoints(path,xxx,xxx,xxx);
//把c的path 转换成 oc的 path 继续进行绘画
UIBezierPath *path1 = [UIBezierPath bezierPathWithCGPath:path]
[path1 addLineToPoints:CGPointsMake(150,50)];
CGCOntextAddPath(ctx,path1.CGPath);
CGContextStrokePath(ctx);
OC语言的方式5种:
UIBezierPath *path = [UIBezierPath bezierPath]//创建路径对象
[path moveToPoints:CGPointsMake(50,50)];
[path addLineToPoints:CGPointsMake(100,100)];
[path stroke];
OC虽然没有获取上下文,其实内部已经封装好了

drawRect

1.代码为什么要写到drawRect中
//写入里面可以确保能获取图形上下文对象
2.rect参数是什么
//是当前view的bounds
3.drawrect什么时候调用
//当view第一次显示的时候会调用
//当view进行重绘的时候调用
4.如何重回
//调用view对象的setNeedDisplay方法
//调用view对象的setNeedsDisplayInRect rect:重绘的区域
5.为什么不能手动调用drawrect
//无法正确拿到图形上下文


绘画矩形
通过类方法直接得到对象,之后渲染即可
[UIBezierPath bezierPathWithRect:CGRectMake(100,100,100,100)];
[path stroke]
或者:CGContextAddRect();
圆角矩形
[UIbezierPath bezierPathWithRoundedRect:Rect cornerRadius:10]stroke
椭圆
[UIbezierPath bezierPathWithOvalInRect:CGRectmake(0,0,200,100)]
或者:CGContextAddEllipseInRect();
圆形
[UIbezierPath bezierPathWithArcCenter:圆心,半径,起始位置,结束位置,是否顺时针]
或者:CGContextAddArc();
设置线宽
[path setLineWidth:宽度]
CGContextSetLineWidth(上下文,宽度);
连接处样式
[path setLineJoinStyle:样式枚举]
CGContextSetLineJoin(上下文,样式枚举);//KCGLineJoinRound/Bevel/Miter
头尾样式
[path setLineCapStyle:样式枚举]
CGContetSetLineCap(上下文,样式枚举);//KCGLineCapSquare
设置颜色
[UIColor blueColor] setStroke];
CGContextSetRGBStrokeColor(上下文,R,G,B,alpha);


额外属性
CGContextClosePath(ctx)//C关闭路径
[path closePath]// OC关闭路径
CGContextStrokePath(ctx);//C为描边渲染
[path stroke];//OC为描边渲染
CGContextFillPath(ctx);//C为填充渲染
[path fill];//OC为描边渲染
CGContextDrawPath(ctx,kCGPathFillStroke);//描边 也填充
//OC也要描边和填充 两个方法都执行
[UIColor redColor]setFill];//设置填充颜色 oc
[UIColor redColor]setStroke];//设置描边颜色 oc


奇偶填充规则
CGContextDrawPath(ctx,kCGPathEOFill); //c
path.usesEvenOddFillRule=YES //oc
//渲染时如第一次覆盖,就不填充,否则再次,就填充

进度条

UISlider组件

发布了15 篇原创文章 · 获赞 0 · 访问量 2556

猜你喜欢

转载自blog.csdn.net/u014270781/article/details/105348517