美文网首页
使用 贝塞尔曲线绘制cell 圆角

使用 贝塞尔曲线绘制cell 圆角

作者: xyZHua | 来源:发表于2021-05-08 12:00 被阅读0次
    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // section row 个数
    NSInteger sectionCount = [tableView numberOfRowsInSection:indexPath.section] - 1;
    // 显示的cell 点击区域,12为距离左右边距
      CGRect bounds = CGRectInset(cell.bounds, 12, 0); 
      // 2.再盖一个 mask
      CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];// 用于蒙板
      // section 只有一个时。
      if (indexPath.row == 0 && indexPath.row == sectionCount) {
          [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:radius].CGPath];   
      } else if (indexPath.row == 0) {
    // 第一个 row 只有顶部圆角 ---> UIRectCornerTopLeft | UIRectCornerTopRight
          [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                              byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)                                          cornerRadii:CGSizeMake(radius, radius)].CGPath];
          } else if (indexPath.row == sectionCount) {
    // 最后一个 row 只有底部圆角 --> UIRectCornerBottomLeft | UIRectCornerBottomRight
              [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                                       byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                             cornerRadii:CGSizeMake(radius, radius)].CGPath]; 
          } else {
    // 中间 row 无圆角
              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/wvludltx.html