画虚线

2018-10-23  本文已影响0人  叫我小黑

绘制一条水平的虚线

- (UIImage *)drawDashLineWithSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置线条终点形状
    CGContextSetLineCap(context, kCGLineCapRound);
    // 设置颜色
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.408 alpha:1.000].CGColor);
    
    CGFloat lengths[] = {10,2};
    /*
     CGContextSetLineDash
     此函数需要四个参数:
     context – 这个不用多说
     phase - 表示在第一个虚线绘制的时候跳过多少个点
     lengths – lengths的值{10,2}表示先绘制10个点,再跳过2个点   如果把lengths值改为{10, 20, 10},则表示先绘制10个点,跳过20个点,绘制10个点,跳过10个点,再绘制20个点,如此反复
     count – lengths数组的长度
     */
    CGContextSetLineDash(context, 0, lengths, 2); //画虚线
    CGContextMoveToPoint(context, 0.0, 2.0); //开始画线
    CGContextAddLineToPoint(context, size.width, 2.0);
    CGContextStrokePath(context);
    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}
上一篇 下一篇

猜你喜欢

热点阅读