美文网首页
iOS绘图及贝塞尔曲线关键知识

iOS绘图及贝塞尔曲线关键知识

作者: Moker_C | 来源:发表于2017-07-27 11:10 被阅读27次

一、如果是自定义一个继承于UIView的子类要在这个子类view上画图的话:1.假如是在drawRect:方法中单纯地画贝塞尔曲线 

- (void)set;

- (void)setFill;

- (void)setStroke;

这三个方法是用来设置这种画线方式所需的边色或者填充色(是类UIColor的实例方法)UIBezierPath *path = [UIBezierPath bezierPath];

[path stroke];//这个方法必须要写的,否则不会画出来(描边)

[path fill];//这个也要写,否则不会画线(填充)

2.假如画完线之后(不论是否在drawRect:方法中),所画贝塞尔曲线的CGPath属性是用作CAShapeLayer的path属性的那么[path stroke]、[path fill]这两个方法可以不要,(1)如果非要写的话画线的代码需要放到

UIGraphicsBeginImageContext(self.view.bounds.size);

UIGraphicsEndImageContext();

这两句代码之间才能避免控制台打印出一串

“Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul  6 14:32:00  TextBeiSaiEr[7433]: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

这些警告。

如果在这两句代码之间用

CGContextRef contextRef =UIGraphicsGetCurrentContext();

CGContextAddEllipseInRect(contextRef, CGRectMake(0,0,100,100));

CGContextSetFillColorWithColor(contextRef,  [UIColor blueColor].CGColor);

 CGContextFillPath(contextRef);

来画线的话,下面这句代码是必须的

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

(2)如果不写[path stroke]、[path fill]这两个方法的话

UIGraphicsBeginImageContext(self.view.bounds.size);

UIGraphicsEndImageContext();

这两句代码也可以不需要

二、如果不自定义UIView的子类

可以直接按一中的第2点来处理

代码如下:(写在viewDidLoad方法中的)

UIBezierPath* path = [UIBezierPath bezierPath];

path.lineWidth =3.0;

path.lineCapStyle =kCGLineCapRound;//终点样式

path.lineJoinStyle =kCGLineJoinRound;//连接点样式

[path moveToPoint:CGPointMake(100,20)];//起点

[path addLineToPoint:CGPointMake(200,40)];//途经点

[path addLineToPoint:CGPointMake(160,140)];//途经点

[path addLineToPoint:CGPointMake(40,140)];//途经点

[path addLineToPoint:CGPointMake(0,40)];//途经点

[path closePath];//闭合

CAShapeLayer * layer = [CAShapeLayer layer];

layer.path = path.CGPath;

layer.backgroundColor = [UIColor clearColor].CGColor;

layer.fillColor = [UIColor clearColor].CGColor;

layer.lineCap =kCALineCapRound;

layer.lineJoin =kCALineJoinRound;

layer.strokeColor = [UIColor redColor].CGColor;

layer.lineWidth = path.lineWidth;

self.view.layer.shouldRasterize = YES;//去线条锯齿

[self.view.layer addSublayer:layer];

相关文章

  • iOS绘图及贝塞尔曲线关键知识

    一、如果是自定义一个继承于UIView的子类要在这个子类view上画图的话:1.假如是在drawRect:方法中单...

  • ios知识点(7)贝塞尔曲线

    贝塞尔曲线扫盲iOS UIBezierPath贝塞尔曲线常用方法iOS UIBezierPath(贝塞尔曲线)iO...

  • iOS 绘图

    1.Quart2D绘图 绘图(CGcontext) 2.贝塞尔曲线 3. 贝塞尔曲线 与 CGcontext 结...

  • 离散进度图层绘制以及点击

    用到知识: 1、贝塞尔曲线 2、​绘图 用法 CGFloat width = [UIScreenmainScree...

  • 贝塞尔曲线

    贝塞尔曲线 用途 贝塞尔曲线奠定了计算机绘图的基础 所有的弧线都是由贝塞尔曲线的运用 原理 依次连接开始数据点 控...

  • Bezier曲线

    参考自iOS开发 贝塞尔曲线UIBezierPath和iOS-UI进阶13 - 贝塞尔曲线和帧动画结合 使用UIB...

  • 开放的多点贝塞尔曲线实现

    开放的多点贝赛尔曲线实现 初识贝塞尔曲线 在实现开放的多点贝塞尔曲线之前,我们先了解下贝塞尔曲线的一些基本的知识....

  • iOS ~ 贝塞尔曲线折线图📈、第三方库CAShapeLaye

    相关链接:1.贝塞尔曲线在 iOS 端的绘图实践[https://www.infoq.cn/article/umm...

  • 初试 贝塞尔曲线

    Android 绘图贝塞尔曲线简单使用 在Android中某些自定义View的时候需要绘制某些曲线,这时候贝塞尔曲...

  • 曲线之美 --贝塞尔曲线

    今天在学习绘图类:UIBezierPath时,发现一个方法用于绘制贝塞尔曲线 //绘制三次贝塞尔曲线 - (voi...

网友评论

      本文标题:iOS绘图及贝塞尔曲线关键知识

      本文链接:https://www.haomeiwen.com/subject/njjifxtx.html