方法一
iOS UITableView section圓角陰影
如果自定義的cell底部有線棍好,則不適合此方法。
距底部有個5pt的偏移丘侠。UIEdgeInsetsMake(0, 0, 5, 0)
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// 圓角角度
CGFloat radius = 4.f;
// 設置cell 背景色為透明
cell.backgroundColor = UIColor.clearColor;
cell.contentView.backgroundColor = UIColor.clearColor;
// 創(chuàng)建兩個layer
CAShapeLayer *normalLayer = [[CAShapeLayer alloc] init];
CAShapeLayer *selectLayer = [[CAShapeLayer alloc] init];
// 獲取顯示區(qū)域大小
CGRect bounds = CGRectInset(cell.bounds, 0, 0);
// cell的backgroundView
UIView *normalBgView = [[UIView alloc] initWithFrame:bounds];
// 獲取每組行數
NSInteger rowNum = [tableView numberOfRowsInSection:indexPath.section];
// 貝塞爾曲線
UIBezierPath *bezierPath = nil;
if (rowNum == 1) {
// 一組只有一行(四個角全部為圓角)
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 {
// 每組不是首位的行不設置圓角
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;
// 把已經繪制好的貝塞爾曲線路徑賦值給圖層惭嚣,然后圖層根據path進行圖像渲染render
normalLayer.path = bezierPath.CGPath;
selectLayer.path = bezierPath.CGPath;
// 設置填充顏色
normalLayer.fillColor = [UIColor whiteColor].CGColor;
// 添加圖層到nomarBgView中
[normalBgView.layer insertSublayer:normalLayer atIndex:0];
normalBgView.backgroundColor = UIColor.clearColor;
cell.backgroundView = normalBgView;
// 替換cell點擊效果
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;
}
方法二
http://www.reibang.com/p/92662061c0dc
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[self shearTableViewSection:cell tableView:tableView IndexPath:indexPath cornerRadius:4 width:0];
}
#pragma mark TableView Section 切圓角
- (void)shearTableViewSection:(UITableViewCell *)cell tableView:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath cornerRadius:(CGFloat)radius width:(CGFloat)width
{
// 圓角弧度半徑
CGFloat cornerRadius = 0.f;
if (radius == 0) {
cornerRadius = 10.f;
}else{
cornerRadius = radius;
}
// 設置cell的背景色為透明饭望,如果不設置這個的話,則原來的背景色不會被覆蓋
cell.backgroundColor = UIColor.clearColor;
cell.contentView.backgroundColor = UIColor.clearColor;
// 創(chuàng)建一個shapeLayer
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init]; //顯示選中
// 創(chuàng)建一個可變的圖像Path句柄澈侠,該路徑用于保存繪圖信息
CGMutablePathRef pathRef = CGPathCreateMutable();
// 獲取cell的size
// 第一個參數,是整個 cell 的 bounds, 第二個參數是距左右兩端的距離,第三個參數是距上下兩端的距離
CGRect bounds;
bounds = CGRectInset(cell.bounds, width, 0);
// CGRectGetMinY:返回對象頂點坐標
// CGRectGetMaxY:返回對象底點坐標
// CGRectGetMinX:返回對象左邊緣坐標
// CGRectGetMaxX:返回對象右邊緣坐標
// CGRectGetMidX: 返回對象中心點的X坐標
// CGRectGetMidY: 返回對象中心點的Y坐標
// 這里要判斷分組列表中的第一行劫侧,每組section的第一行,每組section的中間行
// CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
if ([tableView numberOfRowsInSection:indexPath.section]-1 == 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);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMinX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
}else if (indexPath.row == 0) {
// 初始起點為cell的左下角坐標
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
// 起始坐標為左下角埋涧,設為p板辽,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))為左上角的點奇瘦,設為p1(x1,y1),(CGRectGetMidX(bounds), CGRectGetMinY(bounds))為頂部中點的點劲弦,設為p2(x2,y2)耳标。然后連接p1和p2為一條直線l1,連接初始點p到p1成一條直線l邑跪,則在兩條直線相交處繪制弧度為r的圓角次坡。
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
// 終點坐標為右下角坐標點,把繪圖信息都放到路徑中去,根據這些路徑就構成了一塊區(qū)域了
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
// 初始起點為cell的左上角坐標
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 {
// 添加cell的rectangle信息到path中(不包括圓角)
CGPathAddRect(pathRef, nil, bounds);
}
// 把已經繪制好的可變圖像路徑賦值給圖層砸琅,然后圖層根據這圖像path進行圖像渲染render
layer.path = pathRef;
backgroundLayer.path = pathRef;
// 注意:但凡通過Quartz2D中帶有creat/copy/retain方法創(chuàng)建出來的值都必須要釋放
CFRelease(pathRef);
// 按照shape layer的path填充顏色,類似于渲染render
// layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
layer.fillColor = [UIColor whiteColor].CGColor;
// view大小與cell一致
UIView *roundView = [[UIView alloc] initWithFrame:bounds];
// 添加自定義圓角后的圖層到roundView中
[roundView.layer insertSublayer:layer atIndex:0];
roundView.backgroundColor = UIColor.clearColor;
// cell的背景view
cell.backgroundView = roundView;
}