一分鐘搞定虛線繪制

畫虛線

UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 1)];
    [self.view addSubview:line];
    //繪制虛線
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:line.bounds];
    [shapeLayer setPosition:CGPointMake(line.frame.size.width / 2.0,line.frame.size.height)];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //設(shè)置虛線顏色
    [shapeLayer setStrokeColor:[UIColor blackColor].CGColor];
    //設(shè)置虛線寬度
    [shapeLayer setLineWidth:0.5];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //設(shè)置虛線的線寬及間距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:2], nil]];
    //創(chuàng)建虛線繪制路徑
    CGMutablePathRef path = CGPathCreateMutable();
    //設(shè)置虛線繪制路徑起點
    CGPathMoveToPoint(path, NULL, 0, 0);
    //設(shè)置虛線繪制路徑終點
    CGPathAddLineToPoint(path, NULL, line.frame.size.width, 0);
    //設(shè)置虛線繪制路徑
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //添加虛線
    [line.layer addSublayer:shapeLayer];

畫線框


UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, self.view.frame.size.width - 40, 30)];
    [self.view addSubview:imageView];
    //給控件邊緣加虛線
    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = [UIColor redColor].CGColor;
    border.fillColor = nil;
    border.path = [UIBezierPath bezierPathWithRect:imageView.bounds].CGPath;
    border.frame = imageView.bounds;
    border.lineWidth = .5f;
    border.lineCap = @"square";
    border.lineDashPattern = @[@4, @2];
    [imageView.layer addSublayer:border];

源代碼下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容