【ios學習記錄】-如何定製UITableView的圓角儲存格

來源:互聯網
上載者:User

【ios學習記錄】-如何定製UITableView的圓角儲存格

自從ios7更新以來,UITableView控制項的邊角style由預設圓角變成了直角,更加適應UI扁平化設計的效果了。但對於某種情況來說,如果tableview寬度不是展開到與父視圖等寬,那麼使用直角的tableview則會顯得不好看。如分組列表(group tableview)所示。

而如果此時採用圓角效果的話,則會顯示圓潤溫和,使用者會覺得好看,體驗很好。如所示。

要實現以上分組列表(group tableview)的圓角效果,主要是通過Core Graphics API來實現圖層重繪。於是根據在網上找到的資料和個人搜集的資訊,在這裡主要通過實現UITableViewDelegate協議中的willDisplayCell函數進行自訂cell的操作。

主要思路:
cell背景色設為透明-》建立圖層-》圓角矩形圖層繪製-》把該圖層作為自子圖層賦給UIView-》UIView賦給cell的backgroundView。所謂千言萬語,不如一句代碼,現貼出核心代碼並給出最詳細的注釋。

/** * 將要為每行row繪製cell,可自訂cell顯示方法 */- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    if ([cell respondsToSelector:@selector(tintColor)]) {        if (tableView == self.mainTableView) {            // 圓角弧度半徑            CGFloat cornerRadius = 5.f;            // 設定cell的背景色為透明,如果不設定這個的話,則原來的背景色不會被覆蓋            cell.backgroundColor = UIColor.clearColor;            // 建立一個shapeLayer            CAShapeLayer *layer = [[CAShapeLayer alloc] init];            // 建立一個可變的映像Path控制代碼,該路徑用於儲存繪圖資訊            CGMutablePathRef pathRef = CGPathCreateMutable();            // 擷取cell的size            CGRect bounds = CGRectInset(cell.bounds, 0, 0);            // CGRectGetMinY:返回對象頂點座標            // CGRectGetMaxY:返回對象底點座標            // CGRectGetMinX:返回對象左邊緣座標            // CGRectGetMaxX:返回對象右邊緣座標            // 這裡要判斷分組列表中的第一行,每組section的最後一行,每組section的中間行            BOOL addLine = NO;            // CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);            if (indexPath.row == 0) {                // 初始起點為cell的左下角座標                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));                // 起始座標為左下角,設為p1,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))為左上方的點,設為p1(x1,y1),(CGRectGetMidX(bounds), CGRectGetMinY(bounds))為頂部中點的點,設為p2(x2,y2)。然後串連p1和p2為一條直線l1,串連初始點p到p1成一條直線l,則在兩條直線相交處繪製弧度為r的圓角,並把左邊直線和圓角路徑添加到path中。                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);                // 繪製右上方的圓角,包含頂端直線和右上方圓角並寫入path中                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);                // 右邊直線路徑寫入到path中,構成了一個左上方和右上方為圓角的矩形                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));                addLine = YES;            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {                // 初始起點為cell的左上方座標                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));                // 左邊直線和左下角圓角路徑添加到path                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);               // 底邊直線和右下角圓角路徑添加到path                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);              // 右邊直線添加到path,此時路徑構成一個左下角和右下角為圓角的矩形                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));            } else {                // 中間行,直接添加rectangle資訊到path中(不包括圓角)                CGPathAddRect(pathRef, nil, bounds);                addLine = YES;            }            // 把已經繪製好的可變映像路徑賦值給圖層            layer.path = pathRef;            // 注意:但凡通過Quartz2D中帶有creat/copy/retain方法建立出來的值都必須要釋放            CFRelease(pathRef);            // 按照shape layer的path填充顏色,類似於渲染render            layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;            // 添加分隔線圖層            if (addLine == YES) {                CALayer *lineLayer = [[CALayer alloc] init];                CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);                lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);                // 分隔線顏色取自於原來tableview的分隔線顏色                lineLayer.backgroundColor = tableView.separatorColor.CGColor;                // 添加子圖層                [layer addSublayer:lineLayer];            }            // view大小與cell一致            UIView *roundView = [[UIView alloc] initWithFrame:bounds];            // 添加自訂圓角後的子圖層到roundView中            [roundView.layer insertSublayer:layer atIndex:0];            roundView.backgroundColor = UIColor.clearColor;            //cell的背景view            //cell.selectedBackgroundView = roundView;            cell.backgroundView = roundView;        }    }}

函數分析:

CGPathAddArcToPoint(p1, nil, x1, y1, x2, y2, r);//把起點和終點繪製的圓角路徑添加到graphics path中,包含直線和圓角。


紅色邊部分的路徑會被寫入到path中。

繪製path流程:

// 只舉例列表第一行// 初始起點為cell的左下角座標CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));// 起始座標為左下角,設為p1,(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);

// 繪製右上方的圓角,包含頂端直線和右上方圓角路徑並寫入path中CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);

// 右邊直線路徑寫入到path中,構成了一個左上方和右上方為圓角的矩形,然後調用layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;,按照layer的path繪圖路徑進行顏色填充,相當於渲染的功能CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));

簡單的圓角cell實現OK!整體代碼可在這下載:CornerRadiusTableView

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.