美文网首页iOS常用
iOS-TableViewCell b加圆角

iOS-TableViewCell b加圆角

作者: CocoaJason | 来源:发表于2021-06-13 08:58 被阅读0次
    
    
    - (void)handleWillDisplayCellWithIndexPath:(NSIndexPath *)indexPath
                                  numberOfRows:(NSInteger)count;{
        
        CGRect bounds = CGRectInset(self.bounds, 10, - 1);
        
        /*
         为了切割掉多余的投影
         将顶部和底部的backgroundView进行一些偏移,并且打开裁剪
         */
        if (count > 1) {
            if (indexPath.row == 0) {
                bounds = CGRectMake(10, 3, self.bounds.size.width - 20, self.bounds.size.height + 3);
            } else if (indexPath.row == count - 1) {
                bounds = CGRectMake(10, -5, self.bounds.size.width - 20, self.bounds.size.height + 3);
            }
        }
        UIView *backgroundView = [[UIView alloc] initWithFrame:bounds];
        if (count > 1) {
            backgroundView.clipsToBounds = YES;
        }
        self.backgroundView = backgroundView;
        
        /*
         将背景色都设置为透明色,以方便自定义
         */
        backgroundView.backgroundColor =
        self.backgroundColor =
        self.contentView.backgroundColor = UIColor.clearColor;
        
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        layer.shadowColor = [UIColor cyanColor].CGColor;
        layer.shadowOffset = CGSizeZero;
        layer.shadowOpacity = 0.5;
        layer.fillColor = [UIColor whiteColor].CGColor;
        
        UIBezierPath*path = [UIBezierPath bezierPathWithRect:bounds];
        CGSize cornerRadii = CGSizeZero;
        UIRectCorner corners = UIRectCornerAllCorners;
        
        /*
         1.当数量大于1时
         1.1 第一行实现顶部圆角,最后一行实现底部圆角,中间部分不加圆角
         1.2 当不是第一行的时候,添加分割线
         2.当数量为1时(只有一行)
         2.1实现全圆角
         */
        if (count > 1) {
            if (indexPath.row == 0) {
                cornerRadii = CGSizeMake(10, 10);
                corners = UIRectCornerTopLeft  | UIRectCornerTopRight;
            } else if(indexPath.row == count - 1) {
                cornerRadii = CGSizeMake(10, 10);
                corners = UIRectCornerBottomLeft  | UIRectCornerBottomRight;
            }
            if (corners != UIRectCornerAllCorners) {
                path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:cornerRadii];
            } else {
                path = [UIBezierPath bezierPathWithRect:bounds];
            }
            
            if (indexPath.row != 0) {
                CALayer *border=[[CALayer alloc] init];
                border.frame = CGRectMake(CGRectGetMinX(bounds),0, CGRectGetWidth(bounds), 1/[UIScreen mainScreen].scale);
                border.backgroundColor=[UIColor separatorColor].CGColor;
                [layer addSublayer:border];
            }
        }else {
            cornerRadii = CGSizeMake(10, 10);
            path =[UIBezierPath bezierPathWithRoundedRect:bounds
                                        byRoundingCorners:UIRectCornerAllCorners
                                              cornerRadii:cornerRadii];
        }
        layer.path = path.CGPath;
        [backgroundView.layer insertSublayer:layer atIndex:0];
    }
    
    
    Simulator Screen Shot - iPod touch (7th generation) - 2021-06-13 at 08.57.29.png Simulator Screen Shot - iPod touch (7th generation) - 2021-06-13 at 08.57.33.png

    相关文章

      网友评论

        本文标题:iOS-TableViewCell b加圆角

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