2016-10-18 8 views
3

ImageViewフレームにコーナーのみを描画しようとしています。このために、私はCAShapeLayerとBezierパスを使い、UIImageViewフレームの周りに点線でうまくいきました。しかし、要件はもっと似ていて、コーナーのみを表示する必要があります。私が今まで試したものであり、ImageVIewフレームにコーナーのみを描く

@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

+0

あなたの目的は何ですかこのフレームで?画像のサイズを変更する場合は、https://github.com/yackle/CLImageEditorで画像のサイズを変更することができます。 –

+0

Quartz2Dを使用してCALayerのコーナーを描画することをおすすめします。 – KudoCC

+0

@KudoCCどのように説明できますか? –

答えて

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

+0

これは完全に動作しています。しかし、1つの質問を.. cWidthとは何ですか? –

+1

c幅は4コーナーライン幅です@ SahebSingh –

+0

ohkiee ...ありがとう –

関連する問題