美文网首页
Cell圆角

Cell圆角

作者: 子非鱼7868 | 来源:发表于2022-09-01 23:19 被阅读0次
IMG_0867.PNG

需求:每一个section的第一个row 和最后一个row分别上下圆角 打到图示的效果

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger sectionCount = [tableView numberOfRowsInSection:indexPath.section] - 1;// section row 个数
    CGRect bounds = CGRectInset(cell.bounds, 20, 0); // 显示的cell 点击区域
    // 2.再盖一个 mask
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];// 用于蒙板
    // section 只有一个时。
    if (indexPath.row == 0 && indexPath.row == sectionCount) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:12.0].CGPath];
        // 第一个 row
    } else if (indexPath.row == 0) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                                 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                                       cornerRadii:CGSizeMake(12.0, 20.0)].CGPath];
        
        // 最后一个 row
    } else if (indexPath.row == sectionCount) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                                 byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                       cornerRadii:CGSizeMake(12.0, 12.0)].CGPath];
        // 中间 row
    } else {
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:bounds];
        [maskLayer setPath:path.CGPath];
        
    }
    // 2.mask
    [cell setMaskView:[[UIView alloc] initWithFrame:cell.bounds]];
    [cell.maskView.layer insertSublayer:maskLayer atIndex:0];
    [cell.maskView.layer setMasksToBounds:YES];
    [cell setClipsToBounds:YES];
}

相关文章

网友评论

      本文标题:Cell圆角

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