仅绘制ImageVIew框架的角

时间:2016-10-18 10:30:01

标签: ios objective-c uiview uibezierpath cashapelayer

我正在尝试只绘制ImageView框架的角落。为此,我使用了CAShapeLayer和Bezier路径,并在UIImageView框架周围用虚线工作。但要求更像是,必须只显示角落。我到现在为止尝试的事情是,

@property (strong, nonatomic)CAShapeLayer *border;

_border = [CAShapeLayer layer];
_border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
_border.fillColor = nil;
_border.lineDashPattern = @[@40, @50];
[pImageView.layer addSublayer:_border];
_border.path = [UIBezierPath bezierPathWithRect:pImageView.bounds].CGPath;
_border.frame = pImageView.bounds;

我使用了不同的lineDashPattern但无法得到结果,因为我没有找到适合lineDashPattern的文档。它如何绘制和所有。 任何类型的帮助都是有用的

我想要这样的东西

enter image description here

1 个答案:

答案 0 :(得分:3)

使用以下代码,为我工作:

    //1. For imageView Border
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path =[UIBezierPath bezierPathWithRect:_imgView.bounds].CGPath;
    shapeLayer.strokeColor = [UIColor redColor].CGColor; //etc...
    shapeLayer.lineWidth = 2.0; //etc...
    shapeLayer.position = CGPointMake(0, 0); //etc...
    [_imgView.layer addSublayer:shapeLayer];


    //2. For Outside Corner

    float cWidth=30.0;

    float cSpace=_imgView.frame.size.width+20-(2*cWidth);

    NSNumber *cornerWidth=[NSNumber numberWithFloat:cWidth];
    NSNumber *cornerSpace=[NSNumber numberWithFloat:cSpace];

    CAShapeLayer *_border = [CAShapeLayer layer];

    _border.strokeColor = [UIColor blackColor].CGColor;

    _border.fillColor = nil;

    _border.lineWidth=2.0;

    //imageView frame is (width=240,height=240) and 30+200+30=260 -->10 pixel outside imageView
    _border.lineDashPattern = @[cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth];

    _border.path = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, _imgView.frame.size.width+20, _imgView.frame.size.height+20)].CGPath;

    [_imgView.layer addSublayer:_border];

输出:

img