iOS 繪制虛線的三種方法

  • 方法一:通過 Quartz 2D 在 UIView drawRect: 方法進行繪制虛線
// 可以通過 setNeedsDisplay 方法調用 drawRect:
- (void)drawRect:(CGRect)rect { 
    CGContextRef context =UIGraphicsGetCurrentContext();
    // 設置線條的樣式
    CGContextSetLineCap(context, kCGLineCapRound);
    // 繪制線的寬度
    CGContextSetLineWidth(context, 3.0);
    // 線的顏色
    CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].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 方式繪制虛線
/**
 *  通過 CAShapeLayer 方式繪制虛線
 *
 *  param lineView:       需要繪制成虛線的view
 *  param lineLength:     虛線的寬度
 *  param lineSpacing:    虛線的間距
 *  param lineColor:      虛線的顏色
 *  param lineDirection   虛線的方向  YES 為水平方向, NO 為垂直方向
 **/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayer setBounds:lineView.bounds];

    if (isHorizonal) {

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

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

    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  設置虛線顏色為blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  設置虛線寬度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {

        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  設置線寬,線間距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  設置路徑
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);

    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }

    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把繪制好的虛線添加上來
    [lineView.layer addSublayer:shapeLayer];
}

鏈接:http://blog.csdn.net/ashimar_a/article/details/53033361

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容