ios:画直线和虚线

实线

//获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //画直线,设置路径颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor grayColor].CGColor);
//设置线宽度
    CGContextSetLineWidth(ctx, 2);
    //起始点
    CGContextMoveToPoint(ctx, 0, height);
    //从起始点连线到另一个点
    CGContextAddLineToPoint(ctx, width, height);
    //画线
    CGContextStrokePath(ctx);

虚线

CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //设置虚线颜色
    CGContextSetStrokeColorWithColor(currentContext, LINE_COLOR.CGColor);
    //设置虚线宽度
    CGContextSetLineWidth(currentContext, 1);
    //设置虚线绘制起点
    CGContextMoveToPoint(currentContext, 0, 0);
    //设置虚线绘制终点
    CGContextAddLineToPoint(currentContext, self.frame.origin.x + self.frame.size.width, 0);
    //设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点
    CGFloat arr[] = {3, 1};
    //下面最后一个参数“2”代表排列的个数。
    CGContextSetLineDash(currentContext, 0, arr, 2);
    //画线
    CGContextDrawPath(currentContext, kCGPathStroke);

参考:
1. iOS绘制虚线方式总结
2. iOS 使用Quartz 2D画虚线
3. iOS: 使用CGContextRef,CGPath和UIBezierPath来绘画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值