美文网首页
TableView绘制分区圆角

TableView绘制分区圆角

作者: Bruin_熊先森 | 来源:发表于2017-03-04 11:44 被阅读166次

    _detailedTableView.separatorStyle = UITableViewCellSeparatorStyleNone;(如果分区上下出现不和谐线条请设置这个)

    //绘制分区圆角

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([cell respondsToSelector:@selector(tintColor)]) {

    if (tableView == _detailedTableView) {

    CGFloat cornerRadius = 10.f;

    cell.backgroundColor = UIColor.clearColor;

    CAShapeLayer *layer = [[CAShapeLayer alloc] init];

    CGMutablePathRef pathRef = CGPathCreateMutable();

    // 第一个参数,是整个 cell 的 bounds, 第二个参数是距左右两端的距离,第三个参数是距上下两端的距离

    CGRect bounds = CGRectInset(cell.bounds, 0, 0);

    BOOL addLine = NO;

    //绘制圆角

    if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {

    CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);

    } else if (indexPath.row == 0) {

    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);

    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));

    addLine = YES;

    } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {

    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);

    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));

    } else {

    CGPathAddRect(pathRef, nil, bounds);

    addLine = YES;

    }

    layer.path = pathRef;

    CFRelease(pathRef);

    layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;

    if (addLine == YES) {

    CALayer *lineLayer = [[CALayer alloc] init];

    CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);

    lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);

    lineLayer.backgroundColor = tableView.separatorColor.CGColor;

    [layer addSublayer:lineLayer];

    }

    UIView *testView = [[UIView alloc] initWithFrame:bounds];

    [testView.layer insertSublayer:layer atIndex:0];

    testView.backgroundColor = UIColor.clearColor;

    cell.backgroundView = testView;

    }

    }

    }

    相关文章

      网友评论

          本文标题:TableView绘制分区圆角

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