之前有遇到有人問過一個看起來好像很簡單,但之前卻沒有實踐過的問題,大概就是類似微信我的頁面,tableView 有分區(qū),只是加了另外一個效果,每個分區(qū)有圓角,就是下面這種效果:
效果圖
如果我們像之前一樣,直接設(shè)置:
cell.layer.cornerRadius = 6;
cell.layer.masksToBounds = YES;
那么每個 cell 都會出現(xiàn)圓角,就像下面這種情況:
每個 cell 都會出現(xiàn)圓角
明顯不滿足我們的需求,因為我們的需求簡單來講,就是需要每個分區(qū)的第一個 cell 的左上角跟右上角有圓角,最后一個 cell 的左下角跟右下角有圓角。
那先讓我們來先做前期的準(zhǔn)備工作,把 tableView 先設(shè)置好。
第一步,當(dāng)然是遵循代理協(xié)議,聲明屬性:
遵循代理協(xié)議 聲明屬性
然后,建好數(shù)據(jù),設(shè)置好 tableView:
設(shè)置好 tableView
實現(xiàn) tableView 的協(xié)議方法:
實現(xiàn) tableView 的協(xié)議方法
OK,前期準(zhǔn)備都已搞定,現(xiàn)在就來實現(xiàn)我們想要的效果,那我們要實現(xiàn)UITableViewDelegate協(xié)議中的willDisplayCell方法,在這個方法里面我們需要把 cell的背景設(shè)置成透明,然后自定義 UIView 做為 cell 的 backgroundView,那如果需要選中狀態(tài)的話,還需要再自定義一個 UIView 做為 cell 的 selectedBackgroundView,通過 Core Graphics API 來實現(xiàn) UIView 圖層圓角的繪制。
先把這個協(xié)議方法敲出來:
協(xié)議方法
然后在這個方法里面寫上實現(xiàn)代碼,由于代碼太多,截成兩段吧,方便大家看:
實現(xiàn)代碼段1
實現(xiàn)代碼段2
下面是完整的代碼:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 圓角弧度半徑
CGFloat cornerRadius = 6.f;
// 設(shè)置cell的背景色為透明,如果不設(shè)置這個的話,則原來的背景色不會被覆蓋
cell.backgroundColor = UIColor.clearColor;
// 創(chuàng)建一個shapeLayer
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init]; //顯示選中
// 創(chuàng)建一個可變的圖像Path句柄,該路徑用于保存繪圖信息
CGMutablePathRef pathRef = CGPathCreateMutable();
// 獲取cell的size
// 第一個參數(shù),是整個 cell 的 bounds, 第二個參數(shù)是距左右兩端的距離,第三個參數(shù)是距上下兩端的距離
CGRect bounds = CGRectInset(cell.bounds, 10, 0);
// CGRectGetMinY:返回對象頂點坐標(biāo)
// CGRectGetMaxY:返回對象底點坐標(biāo)
// CGRectGetMinX:返回對象左邊緣坐標(biāo)
// CGRectGetMaxX:返回對象右邊緣坐標(biāo)
// CGRectGetMidX: 返回對象中心點的X坐標(biāo)
// CGRectGetMidY: 返回對象中心點的Y坐標(biāo)
// 這里要判斷分組列表中的第一行,每組section的第一行,每組section的中間行
// CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
if (indexPath.row == 0) {
// 初始起點為cell的左下角坐標(biāo)
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
// 起始坐標(biāo)為左下角,設(shè)為p,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))為左上角的點,設(shè)為p1(x1,y1),(CGRectGetMidX(bounds), CGRectGetMinY(bounds))為頂部中點的點,設(shè)為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);
// 終點坐標(biāo)為右下角坐標(biāo)點,把繪圖信息都放到路徑中去,根據(jù)這些路徑就構(gòu)成了一塊區(qū)域了
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
// 初始起點為cell的左上角坐標(biāo)
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);
// 添加一條直線,終點坐標(biāo)為右下角坐標(biāo)點并放到路徑中去
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
} else {
// 添加cell的rectangle信息到path中(不包括圓角)
CGPathAddRect(pathRef, nil, bounds);
}
// 把已經(jīng)繪制好的可變圖像路徑賦值給圖層,然后圖層根據(jù)這圖像path進(jìn)行圖像渲染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;
// 以上方法存在缺陷當(dāng)點擊cell時還是出現(xiàn)cell方形效果,因此還需要添加以下方法
// 如果你 cell 已經(jīng)取消選中狀態(tài)的話,那以下方法是不需要的.
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:bounds];
backgroundLayer.fillColor = [UIColor cyanColor].CGColor;
[selectedBackgroundView.layer insertSublayer:backgroundLayer atIndex:0];
selectedBackgroundView.backgroundColor = UIColor.clearColor;
cell.selectedBackgroundView = selectedBackgroundView;
}
寫完,直接運(yùn)行,我們一開始的那張圖的效果就出來了!