美文网首页iOS
UITableView Section增加圆角

UITableView Section增加圆角

作者: 风云永杰 | 来源:发表于2020-08-25 10:06 被阅读0次

    先上效果图:


    image.png

    因为有的section里面有HeaderView有的没有HeaderView,所以切图角的时候需要判断这种情况.
    圆角直接用的layer的mask实现
    OK,直接上代码吧:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        //圆率
        CGFloat cornerRadius = 8;
        //大小
        CGRect bounds = cell.bounds;
        //行数
        NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
        UIView *headerView;
        if ([self respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
            headerView=[self tableView:tableView viewForHeaderInSection:indexPath.section];
        }
        
        //绘制曲线
        UIBezierPath *bezierPath = nil;
        if (headerView) {
            if (indexPath.row == 0 && numberOfRows == 1) {
                //一个为一组时,四个角都为圆角
                bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            }else
            if (indexPath.row == numberOfRows - 1) {
                //为组的最后一行,左下、右下角为圆角
                bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            } else {
                //中间的都为矩形
                bezierPath = [UIBezierPath bezierPathWithRect:bounds];
            }
        }else{
            if (indexPath.row == 0 && numberOfRows == 1) {
                //一个为一组时,四个角都为圆角
                bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            } else if (indexPath.row == 0) {
                //为组的第一行时,左上、右上角为圆角
                bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            } else if (indexPath.row == numberOfRows - 1) {
                //为组的最后一行,左下、右下角为圆角
                bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            } else {
                //中间的都为矩形
                bezierPath = [UIBezierPath bezierPathWithRect:bounds];
            }
        }
        
        //新建一个图层
        CAShapeLayer *layer = [CAShapeLayer layer];
        //图层边框路径
        layer.path = bezierPath.CGPath;
        cell.layer.mask=layer;
    }
    
    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        //圆率
        CGFloat cornerRadius = 8;
        //大小
        CGRect bounds = view.bounds;
        
        //绘制曲线
        UIBezierPath *bezierPath = nil;
        //为组的第一行时,左上、右上角为圆角
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        //新建一个图层
        CAShapeLayer *layer = [CAShapeLayer layer];
        //图层边框路径
        layer.path = bezierPath.CGPath;
        view.layer.mask=layer;
    }
    

    相关文章

      网友评论

        本文标题:UITableView Section增加圆角

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