iOS开发 给view绘制虚线边框

给view绘制虚线边框

- (void)layoutBorderToLayer:(UIView *)view {
    CAShapeLayer *border = [CAShapeLayer layer];
    // 线条颜色
    border.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.8].CGColor;
    border.fillColor = nil;

    CGFloat viewHeight = view.frame.size.height;
    CGFloat viewWidth = view.frame.size.width;

    border.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0, 0.0, viewWidth, viewHeight) cornerRadius:view.layer.cornerRadius].CGPath;
    border.frame = view.bounds;
    // 不要设太大 不然看不出效果
    border.lineWidth = 1.0f;
    border.lineCap = @"square";
    // 第一个是 线条长度 第二个是间距 nil时为实线
    border.lineDashPattern = @[@5, @5];
    [view.layer addSublayer:border];
}

使用

    self.testView.layer.cornerRadius = 27.5f;
    self.testView.clipsToBounds = YES;
    [self layoutBorderToLayer:self.testView];

你可能感兴趣的:(IOS)