美文网首页
iOS 绘制虚线

iOS 绘制虚线

作者: 林希品 | 来源:发表于2021-11-05 09:46 被阅读0次

    iOS 绘制虚线

    - (void)drawLineByImageView:(UIImageView *)imageView {

        UIGraphicsBeginImageContext(imageView.frame.size);   //开始画线 划线的frame

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

        //设置线条终点形状

        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

        CGContextRef line = UIGraphicsGetCurrentContext();

        // 设置颜色

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

        CGFloat lengths[] = {5,2};//先画4个点再画2个点

        CGContextSetLineDash(line,0, lengths,2);//注意2(count)的值等于lengths数组的长度

        CGContextMoveToPoint(line, 0.0, 2.0);    //开始画线

        CGContextAddLineToPoint(line,imageView.frame.size.width,2.0);    CGContextStrokePath(line);

        // UIGraphicsGetImageFromCurrentImageContext()返回的就是image

        UIImage *image =   UIGraphicsGetImageFromCurrentImageContext();

        imageView.image = image;

    }

    相关文章

      网友评论

          本文标题:iOS 绘制虚线

          本文链接:https://www.haomeiwen.com/subject/thszaltx.html