iOS绘图系列(二)画直线

我们可以通过Core Graphics 和 UIKit框架画直线

CGContextRef ctx = UIGraphicsGetCurrentContext();  
    
  CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);//设置线的颜色  
  CGContextSetLineWidth(ctx, 2);//设置线的宽度  
  CGContextSetLineCap(ctx, kCGLineCapRound);//设置线的起始端的样式  
  CGContextSetLineJoin(ctx, kCGLineJoinRound);//设置线的连接样式  
    
  //画一条线  
  CGContextMoveToPoint(ctx, 10, 50);  
  CGContextAddLineToPoint(ctx, 100, 50);  
  //同时依次画多条线  
  CGPoint lines[] = {  
      CGPointMake(10.0, 90.0),  
CGPointMake(70.0, 60.0),  
CGPointMake(130.0, 90.0),  
CGPointMake(190.0, 60.0),  
CGPointMake(250.0, 90.0),  
CGPointMake(310.0, 60.0),  
  };  
  //参数二:线数组,参数三:线的个数  
  CGContextAddLines(ctx, lines, sizeof(lines)/sizeof(lines[0]));  
    
  CGContextStrokePath(ctx);
UIBezierPath *path = [UIBezierPath bezierPath];  
[path moveToPoint:CGPointMake(10, 50)];  
[path addLineToPoint:CGPointMake(100, 50)];  
path.lineWidth = 2;  
[[UIColor blackColor] setStroke];  
[path stroke];  
  
path = [UIBezierPath bezierPath];  
[path moveToPoint:CGPointMake(10.0, 90.0)];  
[path addLineToPoint:CGPointMake(70.0, 60.0)];  
[path addLineToPoint:CGPointMake(130.0, 90.0)];  
[path addLineToPoint:CGPointMake(190.0, 60.0)];  
[path addLineToPoint:CGPointMake(250.0, 90.0)];  
[path addLineToPoint:CGPointMake(310.0, 60.0)];  
path.lineWidth = 2;  
[path stroke];  

还可以画虚线

CGContext中

//设置虚线  
CGFloat length[] = {10,5,10};  
CGContextSetLineDash(ctx, 8, length, 3);  

UIBezierPath中

CGFloat length[] = {10,5,10};  
[path setLineDash:length count:3 phase:8];  

iOS绘图系列(一)概述

iOS绘图系列(二)画直线 CGContextMoveToPoint、CGContextAddLineToPoint、CGContextAddLines

iOS绘图系列(三)画弧线 CGContextAddArc、CGContextAddArcToPoint、CGContextAddCurveToPoint、UIBezierPath

iOS绘图系列(四)绘制文字和图像 CGContextDrawImage、drawInRect:、drawAtPoint:、UIGraphicsBeginImageContext

iOS绘图系列(五)绘制渐变 CGContextDrawLinearGradient、CGContextDrawRadialGradient、CAGradientLayer

iOS绘图系列(六)利用CAShapeLayer、UIBezierPath、CGPath绘制图像

转载于:https://my.oschina.net/gwlCode/blog/863096

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值