美文网首页iOS Developer
iOS 如何在九宫UICollectionView中添加边界

iOS 如何在九宫UICollectionView中添加边界

作者: Atonine | 来源:发表于2017-08-25 21:30 被阅读0次

如果用layer.border来设置九宫格,当你设置宽度为0.5,两个cell挨在一块,会使他们的边框宽度增加一倍,如果在自定义的cell中代码加上view,也会引起上下左右的view因cell所处的方位的不同从而位置不同而引起麻烦。而且还会因为cell的重用而引起一些错误,
所以查阅了部分资料总结了以下方法
我用的是自定义一个cell的方法
用枚举方法来表示边框的方位

//MyCell.m
typedef enum : NSUInteger {
    SYCellBorderDirectionTop = 1 << 0,
    SYCellBorderDirectionBottom = 1<<1,
    SYCellBorderDirectionRight = 1 <<2,
    SYCellBorderDirectionLeft = 1<<3,
    
} SYCellBorderDirection;

下面就是如何,在各个方位来添加border


//MyCell.m
-(void)setBorderWithDirection:(SYCellBorderDirection)direction{
    CAShapeLayer *layer = [[CAShapeLayer alloc]init];
    layer.lineWidth = 0.5;
    layer.borderWidth =0.5;
    CGRect rect = self.contentView.bounds;
    layer.strokeColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0  blue:224.0/255.0  alpha:1].CGColor;
    
    
    UIBezierPath*path = [[UIBezierPath alloc]init];
    
   // SYCellBorderDirection *dir = SYCellBorderDirectionTop|SYCellBorderDirectionBottom|SYCellBorderDirectionRight|SYCellBorderDirectionLeft
    if (direction & SYCellBorderDirectionTop) {
        [path moveToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(rect.size.width, 0)];
        
        layer.path = path.CGPath;
        
        [self.contentView.layer addSublayer:layer];
        
    }
    if (direction & SYCellBorderDirectionBottom) {
        [path moveToPoint:CGPointMake(0, rect.size.height)];
        
        [path addLineToPoint:CGPointMake(rect.size.width, rect.size.height)];
        
        layer.path = path.CGPath;
        
        [self.contentView.layer addSublayer:layer];
       
    }
    if (direction & SYCellBorderDirectionRight) {
        [path moveToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(0, rect.size.height)];
        
        layer.path = path.CGPath;
        
        [self.contentView.layer addSublayer:layer];
        
    }
    if (direction & SYCellBorderDirectionLeft) {
        [path moveToPoint:CGPointMake(rect.size.width, 0)];
        [path addLineToPoint:CGPointMake(rect.size.width, rect.size.height)];
        
        layer.path = path.CGPath;
        
        [self.contentView.layer addSublayer:layer];
      
    }
    
}



最后我们就可以因为位置的不同来设置边框
在MyColleViewController.m里给位置不同的cell的tag赋值

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
     ...
     cell.tag = indexPath.row;
     ...
}

然后cell根据tag的不同来添加边框

//MyCell.m

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    if ((self.tag/3)==0) {
        [self setBorderWithDirection:SYCellBorderDirectionBottom|SYCellBorderDirectionRight|SYCellBorderDirectionTop];
    }if ((self.tag/3)>0) {
        [self setBorderWithDirection:SYCellBorderDirectionBottom|SYCellBorderDirectionRight];
    }
}


相关文章

网友评论

    本文标题: iOS 如何在九宫UICollectionView中添加边界

    本文链接:https://www.haomeiwen.com/subject/opupdxtx.html