2016-11-22 7 views
0

カスタムUICollectionViewCellの周りに破線の境界線が表示されるようにするため、レイヤーとベジェパスを使用してみましたが、いずれのアプローチも役に立たなかった。あなたにUICollectionViewCell破線のボーダー

答えて

0

UICollectionViewCell'sサブクラス

static CGFloat const kDashedBorderWidth  = (2.0f); 
static CGFloat const kDashedPhase   = (0.0f); 
static CGFloat const kDashedLinesLength[] = {4.0f, 2.0f}; 
static size_t const kDashedCount   = (2.0f); 



-(void)drawRect:(CGRect)rect 
{ 


    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, kDashedBorderWidth); 
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextSetLineDash(context, kDashedPhase, kDashedLinesLength, kDashedCount) ; 
    CGContextAddRect(context, rect); 
    CGContextStrokePath(context); 

} 
0

は、あなたのクラスでこのメソッドを追加します。

- (CAShapeLayer *) addDashedBorderWithColor: (CGColorRef)color withSize:(CGSize)size{ 
    CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 

    CGSize frameSize = size; 

    CGRect shapeRect = CGRectMake(0.0f, 0.0f, frameSize.width, frameSize.height); 
    [shapeLayer setBounds:shapeRect]; 
    [shapeLayer setPosition:CGPointMake(frameSize.width/2,frameSize.height/2)]; 

    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]]; 
    [shapeLayer setStrokeColor:color]; 
    [shapeLayer setLineWidth:5.0f]; 
    [shapeLayer setLineJoin:kCALineJoinRound]; 
    [shapeLayer setLineDashPattern: 
    [NSArray arrayWithObjects:[NSNumber numberWithInt:10], 
     [NSNumber numberWithInt:5], 
     nil]]; 
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0]; 
    [shapeLayer setPath:path.CGPath]; 

    return shapeLayer; 
} 

cellForRowAtIndexPathメソッドでは、以下のコードを書きます。

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier"@"cell"]; 
    //Do what you want to set in your collectionViewCell 
    CAShapeLayer *maskLayer= [self addDashedBorderWithColor:[UIColor whiteColor].CGColor withSize:cell.borderView.frame.size]; 
    cell.layer.mask = maskLayer; 
+0

ビューコントローラのself.sizeはエラーですか? –

+0

私はあなたが使用しているyカスタムセルでボーダービューとしてプロパティを持っていません。 –

+0

これは、この境界線を描画したい自分のビュー名でこれを変更することを意味します。 私はちょうど私のdummuyオブジェクトであなたを送信しています。 –