iOS UITableView設置section圓角

分組圓角.gif

這個一直覺得簡單又不知道從哪兒下手的功能,今天有空,找了下資料動手做一做
主要利用UITableViewDelegatewillDisplayCell方法結合UIBezierPath繪制顯示的圓角

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  // 圓角角度
    CGFloat radius = 10.f;
    // 設置cell 背景色為透明
    cell.backgroundColor = UIColor.clearColor;
    // 創建兩個layer
    CAShapeLayer *normalLayer = [[CAShapeLayer alloc] init];
    CAShapeLayer *selectLayer = [[CAShapeLayer alloc] init];
    // 獲取顯示區域大小
    CGRect bounds = CGRectInset(cell.bounds, 10, 0);
    // 獲取每組行數
    NSInteger rowNum = [tableView numberOfRowsInSection:indexPath.section];
    // 貝塞爾曲線
    UIBezierPath *bezierPath = nil;

每組考慮只有一行和有多行的情況,若行數為1,則這個cell的每個角都是圓角,否則第一行的左上右上為圓角,最后一行的左下右下為圓角

    if (rowNum == 1) {
        // 一組只有一行(四個角全部為圓角)
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds
                                           byRoundingCorners:UIRectCornerAllCorners
                                                 cornerRadii:CGSizeMake(radius, radius)];
    } else {
        if (indexPath.row == 0) {
            // 每組第一行(添加左上和右上的圓角)
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds
                                               byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                                     cornerRadii:CGSizeMake(radius, radius)];
            
        } else if (indexPath.row == rowNum - 1) {
            // 每組最后一行(添加左下和右下的圓角)
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds
                                               byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                     cornerRadii:CGSizeMake(radius, radius)];
        } else {
            // 每組不是首位的行不設置圓角
            bezierPath = [UIBezierPath bezierPathWithRect:bounds];
        }
    }

然后將貝塞爾曲線的路徑賦值給圖層,并將圖層添加到view中

    // 把已經繪制好的貝塞爾曲線路徑賦值給圖層,然后圖層根據path進行圖像渲染render
    normalLayer.path = bezierPath.CGPath;
    selectLayer.path = bezierPath.CGPath;
    
    
    UIView *nomarBgView = [[UIView alloc] initWithFrame:bounds];
    // 設置填充顏色
    normalLayer.fillColor = [UIColor colorWithWhite:0.95 alpha:1.0].CGColor;
    // 添加圖層到nomarBgView中
    [nomarBgView.layer insertSublayer:normalLayer atIndex:0];
    nomarBgView.backgroundColor = UIColor.clearColor;
    cell.backgroundView = nomarBgView;

此時圓角顯示就完成了,但是如果沒有取消cell的點擊效果,還是會出現一個灰色的長方形的形狀,再用上面創建的selectLayercell添加一個selectedBackgroundView

    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;
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Swift1> Swift和OC的區別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,141評論 1 32
  • 之前有遇到有人問過一個看起來好像很簡單,但之前卻沒有實踐過的問題,大概就是類似微信我的頁面,tableView 有...
    Aiya閱讀 2,000評論 0 1
  • 前言 說起優化,簡直是博大精深。話不多說,筆者今天梳理的內容,UITableView的性能優化。先說一下table...
    陌路賣醬油閱讀 9,663評論 0 57
  • 優化UITableView常用的方式有:Cell重用、緩存Cell高度、Cell數據資源緩存、渲染、減少視圖數目、...
    云中追月閱讀 1,232評論 0 3
  • iOS最常用的控件-UITableView,基本用法就不多說了,大家應該都知道,當然對于它的優化大家也應該都有所了...
    星星星宇閱讀 2,453評論 0 19