iOS开发绘制虚线的方法

iOS开发绘制虚线的方法

方法一:通过Quartz 2D 在 UIView drawRect:方法进行绘制虚线

- (void)drawRect:(CGRect)rect {// 可以通过 setNeedsDisplay 方法调用 drawRect:

    // Drawing code

    CGContextRef context =UIGraphicsGetCurrentContext();

    // 设置线条的样式

    CGContextSetLineCap(context, kCGLineCapRound);

    // 绘制线的宽度

    CGContextSetLineWidth(context,3.0);

    // 线的颜色

    CGContextSetStrokeColorWithColor(context, [UIColororangeColor].CGColor);

    // 开始绘制

    CGContextBeginPath(context);

    // 设置虚线绘制起点

    CGContextMoveToPoint(context,10.0,20.0);

    // lengths的值{10,10}表示先绘制10个点,再跳过10个点,如此反复

    CGFloat lengths[] = {10,10};

    // 虚线的起始点

    CGContextSetLineDash(context,0, lengths,2);

    // 绘制虚线的终点

    CGContextAddLineToPoint(context,310.0,20.0);

    // 绘制

    CGContextStrokePath(context);

    // 关闭图像

    CGContextClosePath(context);

}

方法二:通过 Quartz 2D 在 UIImageView 绘制虚线

/**

*  通过 Quartz 2D 在 UIImageView 绘制虚线

*

*  param imageView 传入要绘制成虚线的imageView

*  return

*/

- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {

    // 开始划线 划线的frame

    UIGraphicsBeginImageContext(imageView.frame.size);    

    [imageView.image drawInRect:CGRectMake(0,0, imageView.frame.size.width, imageView.frame.size.height)];

    // 获取上下文

    CGContextRef line = UIGraphicsGetCurrentContext();

    // 设置线条终点的形状

    CGContextSetLineCap(line, kCGLineCapRound);

    // 设置虚线的长度 和 间距

    CGFloat lengths[] = {5,5};   

     CGContextSetStrokeColorWithColor(line, [UIColor greenColor].CGColor);

    // 开始绘制虚线

    CGContextSetLineDash(line,0, lengths,2);   

     CGContextMoveToPoint(line,0.0,2.0);   

     CGContextAddLineToPoint(line,300,2.0);   

     CGContextStrokePath(line);

    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image

    return UIGraphicsGetImageFromCurrentImageContext();

}

方法三:通过CAShapeLayer 方式绘制虚线

/**

** lineView:      需要绘制成虚线的view

** lineLength:    虚线的宽度

** lineSpacing:    虚线的间距

** lineColor:      虚线的颜色

**/

- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor

{

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayer setBounds:lineView.bounds];

    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];

    [shapeLayer setFillColor:[UIColor clearColor].CGColor];

    //  设置虚线颜色

    [shapeLayer setStrokeColor:lineColor.CGColor];

    //  设置虚线宽度

    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];

    [shapeLayer setLineJoin:kCALineJoinRound];

    //  设置线宽,线间距

    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];

    //  设置路径

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 0, 0);

    CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);

    [shapeLayer setPath:path];

    CGPathRelease(path);

    //  把绘制好的虚线添加上来

    [lineView.layer addSublayer:shapeLayer];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 发改委没有新的额度批复 2. 中国财政部:加强地方政府债券发行计划管理,提升地方政府债券发行定价市场化水平;...
    宋偲瑄阅读 323评论 0 0
  • 姓名:杨霞 南京京科医院【日精进打卡第365】 第394期 努力二组 【知-学习】 《六项精进》0遍 共22遍 《...
    努力二组阅读 238评论 0 0
  • 春节刚刚结束,很多读者大概和我一样,因为节日期间吃香喝辣,导致身上多长了几斤肉,那今年接下去的主题大概就是减肥健身...
    117他爸阅读 987评论 6 12
  • 逢年不回家 无关幼稚 在外勤加努力 时间无终止 谨记。
    怪物妹妮娜阅读 289评论 0 0
  • 前几天去吃了高中时爱吃的漳州卤面,面条和加料看起来都和以前一样,就是汤汁和卤大肠不入味,都说一个人的味蕾基本就是小...
    年少影追阅读 195评论 0 0