oc的方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// 圓角角度
CGFloat radius = 10.f;
// 設(shè)置cell 背景色為透明
cell.backgroundColor = UIColor.clearColor;
// 創(chuàng)建兩個(gè)layer
CAShapeLayer *normalLayer = [[CAShapeLayer alloc] init];
CAShapeLayer *selectLayer = [[CAShapeLayer alloc] init];
// 獲取顯示區(qū)域大小
CGRect bounds = CGRectInset(cell.bounds, kScaleX * 20, 0);
// cell的backgroundView
UIView *normalBgView = [[UIView alloc] initWithFrame:bounds];
// 獲取每組行數(shù)
NSInteger rowNum = [tableView numberOfRowsInSection:indexPath.section];
// 貝塞爾曲線
UIBezierPath *bezierPath = nil;
if (rowNum == 1) {
// 一組只有一行(四個(gè)角全部為圓角)
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
normalBgView.clipsToBounds = NO;
}else {
normalBgView.clipsToBounds = YES;
if (indexPath.row == 0) {
normalBgView.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(-5, 0, 0, 0));
CGRect rect = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, 0, 0, 0));
// 每組第一行(添加左上和右上的圓角)
bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(radius, radius)];
}else if (indexPath.row == rowNum - 1) {
normalBgView.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, -5, 0));
CGRect rect = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 5, 0));
// 每組最后一行(添加左下和右下的圓角)
bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(radius, radius)];
}else {
// 每組不是首位的行不設(shè)置圓角
bezierPath = [UIBezierPath bezierPathWithRect:bounds];
}
}
// 陰影
normalLayer.shadowColor = [UIColor blackColor].CGColor;
normalLayer.shadowOpacity = 0.2;
normalLayer.shadowOffset = CGSizeMake(0, 0);
normalLayer.path = bezierPath.CGPath;
normalLayer.shadowPath = bezierPath.CGPath;
// 把已經(jīng)繪制好的貝塞爾曲線路徑賦值給圖層,然后圖層根據(jù)path進(jìn)行圖像渲染render
normalLayer.path = bezierPath.CGPath;
selectLayer.path = bezierPath.CGPath;
// 設(shè)置填充顏色
normalLayer.fillColor = [UIColor whiteColor].CGColor;
// 添加圖層到nomarBgView中
[normalBgView.layer insertSublayer:normalLayer atIndex:0];
normalBgView.backgroundColor = UIColor.clearColor;
cell.backgroundView = normalBgView;
// 替換cell點(diǎn)擊效果
UIView *selectBgView = [[UIView alloc] initWithFrame:bounds];
selectLayer.fillColor = [UIColor colorWithWhite:0.95 alpha:1.0].CGColor;
[selectBgView.layer insertSublayer:selectLayer atIndex:0];
selectBgView.backgroundColor = UIColor.clearColor;
cell.selectedBackgroundView = selectBgView;
}
swift
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// 圓角角度
let radius = 10.zoom()
// 設(shè)置cell 背景色為透明
cell.backgroundColor = .clear
// 創(chuàng)建兩個(gè)layer
let normalLayer = CAShapeLayer()
let selectLayer = CAShapeLayer()
// 獲取顯示區(qū)域大小
let bounds = cell.bounds.insetBy(dx: 15.zoom(), dy: 0)
// cell的backgroundView
let normalBgView = UIView(frame: bounds)
// 獲取每組行數(shù)
let rowNum = tableview.numberOfRows(inSection: indexPath.section)
var bezierPath:UIBezierPath
normalBgView.clipsToBounds = true
if indexPath.row == 0 {
// 每組第一行
normalBgView.frame = bounds.inset(by: UIEdgeInsets(top: -5.zoom(), left: 0, bottom: 0, right: 0))
let rect = bounds.inset(by: UIEdgeInsets(top: 5.zoom(), left: 0, bottom: 0, right: 0))
// 每組第一行(添加左上和右上的圓角)
bezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners: [.topLeft,.topRight], cornerRadii: CGSize(width: radius, height: radius))
}else if indexPath.row == rowNum - 1 {
// 每組最后一行
normalBgView.frame = bounds.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: -2.zoom(), right: 0))
let rect = bounds.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 2.zoom(), right: 0))
// 每組最后一行(添加左下和右下的圓角)
bezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners: [.bottomLeft,.bottomRight], cornerRadii: CGSize(width: radius, height: radius))
}else{
// 每組不是首位的行不設(shè)置圓角
bezierPath = UIBezierPath(rect: bounds)
}
// 陰影
normalLayer.shadowColor = UIColor.black.cgColor
normalLayer.shadowOpacity = 0.2
normalLayer.shadowOffset = CGSize(width: 0, height: 0)
normalLayer.path = bezierPath.cgPath
normalLayer.shadowPath = bezierPath.cgPath
// 把已經(jīng)繪制好的貝塞爾曲線路徑賦值給圖層,然后圖層根據(jù)path進(jìn)行圖像渲染render
normalLayer.path = bezierPath.cgPath
selectLayer.path = bezierPath.cgPath
// 設(shè)置填充顏色
normalLayer.fillColor = UIColor.white.cgColor
// 添加圖層到nomarBgView中
normalBgView.layer .insertSublayer(normalLayer, at: 0)
normalBgView.backgroundColor = UIColor.clear
cell.backgroundView = normalBgView
// 替換cell點(diǎn)擊效果
let selectBgView = UIView(frame: bounds)
selectLayer.fillColor = UIColor.init(white: 0.95, alpha: 1).cgColor
selectBgView.layer .insertSublayer(selectLayer, at: 0)
selectBgView.backgroundColor = UIColor.clear
cell.selectedBackgroundView = selectBgView
}