iOS-给View添加虚线

Quartz 2D绘制

- (void)addBorderToLayer2:(UIView*)view

{

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayersetBounds:view.bounds];

    [shapeLayersetPosition:CGPointMake(CGRectGetWidth(view.frame) / 2, CGRectGetHeight(view.frame)/2)];


    [shapeLayersetStrokeColor:[UIColor lightGrayColor].CGColor];

    [shapeLayersetLineWidth:0.5];

    //  设置线宽,线间距

    [shapeLayersetLineDashPattern:@[@6,@10]];

    //  设置路径

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 0, 0);

    if (CGRectGetWidth(view.frame) > CGRectGetHeight(view.frame)) {

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

    }else{

        CGPathAddLineToPoint(path, NULL, 0,CGRectGetHeight(view.frame));

    }

    [shapeLayersetPath:path];

    CGPathRelease(path);


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

    [view.layeraddSublayer:shapeLayer];

}

你可能感兴趣的:(iOS-给View添加虚线)