iOS 用CGContextRef画虚线

-画虚线需要用到函数:

CGContextSetLineDash

此函数需要四个参数:

  • context 
  • phase 
  • lengths – 指明虚线是如何交替绘制
  • count – lengths数组的长度

-(void)drawRect:(CGRect)rect{


    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextBeginPath(context);

    CGContextSetLineWidth(context, 2.0);

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);

    CGFloat lengths[] = {10,10};

    CGContextSetLineDash(context, 0, lengths,2);

    CGContextMoveToPoint(context, 10.0, 20.0);

    CGContextAddLineToPoint(context, 310.0,20.0);

    CGContextStrokePath(context);

    CGContextClosePath(context);

    

    //CGFloat lengths[] = {10,5};

    CGContextSetLineDash(context, 0, lengths, 2);

    CGContextMoveToPoint(context, 0.0, 20.0);

    CGContextAddLineToPoint(context, 310.0, 20.0);

    CGContextStrokePath(context);

    

    CGContextSetLineDash(context, 5, lengths, 2);

    CGContextMoveToPoint(context, 0.0, 40.0);

    CGContextAddLineToPoint(context, 310.0, 40.0);

    CGContextStrokePath(context);

    

    CGContextSetLineDash(context, 8, lengths, 2);

    CGContextMoveToPoint(context, 0.0, 60.0);

    CGContextAddLineToPoint(context, 310.0, 60.);

    CGContextStrokePath(context);


}


由于lengths值为{10,5},第一条线就是绘制10,跳过5,反复绘制。

第二条线的phase值为5,则首先绘制【10减去5】,再跳过5,绘制10,反复绘制。

第三条给也如此,先绘制2,再跳过5,如此反复。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

午夜小学徒丶磊

你得鼓励是我最大的动力,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值