您的位置:首页 > 移动开发 > IOS开发

H含金量 iOS绘图及贝塞尔曲线关键知识

2016-07-06 14:56 423 查看
一、如果是自定义一个继承于UIView的子类

要在这个子类view上画图的话:

1.假如是在drawRect:方法中单纯地画贝塞尔曲线

// Set the color: Sets the fill and stroke colors in the current drawing context. Should be implemented by subclassers.

- (void)set;

// Set the fill or stroke colors individually. These should be implemented by subclassers.

- (void)setFill;

- (void)setStroke;

这三个方法是用来设置这种画线方式所需的边色或者填充色(是类UIColor的实例方法)

UIBezierPath* aPath = [UIBezierPathbezierPath];

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

[aPath fill];(填充)

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

(1)如果非要写的话画线的代码需要放到

UIGraphicsBeginImageContext(self.view.bounds.size);

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

Jul  6 14:32:00  TextBeiSaiEr[7433] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.


这些警告。

如果在这两句代码之间要用

CGContextRef con =UIGraphicsGetCurrentContext();

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

CGContextSetFillColorWithColor(con, [UIColorblueColor].CGColor);

CGContextFillPath(con);

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

UIImage* im =
UIGraphicsGetImageFromCurrentImageContext();
(2)如果不写[aPath stroke]、[aPath
fill]这两个方法的话

UIGraphicsBeginImageContext(self.view.bounds.size);

UIGraphicsEndImageContext();
这两句代码不需要

二、如果不自定义UIView的子类
可以直接按一中的第2点来处理

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

    UIBezierPath* aPath = [UIBezierPathbezierPath];

    aPath.lineWidth =5.0;

    

    aPath.lineCapStyle =kCGLineCapRound; 
//线条拐角

    aPath.lineJoinStyle =kCGLineCapRound; 
//终点处理

    

    // Set the starting point of the shape.

    [aPath moveToPoint:CGPointMake(100.0,20.0)];

    

    // Draw the lines

    [aPath addLineToPoint:CGPointMake(200.0,40.0)];

    [aPath addLineToPoint:CGPointMake(160,140)];

    [aPath addLineToPoint:CGPointMake(40.0,140)];

    [aPath addLineToPoint:CGPointMake(0.0,40.0)];

    [aPath closePath];//第五条线通过调用closePath方法得到的

    

    CAShapeLayer * slayer = [CAShapeLayerlayer];

    slayer.path = aPath.CGPath;

    slayer.backgroundColor = [UIColorclearColor].CGColor;

    slayer.fillColor = [UIColorclearColor].CGColor;

    slayer.lineCap =kCALineCapRound;

    slayer.lineJoin =kCALineJoinRound;

    slayer.strokeColor = [UIColorredColor].CGColor;

    slayer.lineWidth = aPath.lineWidth;

    

    [self.view.layeraddSublayer:slayer];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: