UITableview重新繪制cell 圓角
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// 圓?弧度半徑
let cornerRadius: CGFloat = 10
// 設(shè)置cell的背景?為透明,如果不設(shè)置這個(gè)的話,則原來的背景?不會(huì)被覆蓋
cell.backgroundColor = .clear
// 創(chuàng)建?個(gè)shapeLayer
let layer = CAShapeLayer()
let backgroundLayer = CAShapeLayer() //顯?選中
// 創(chuàng)建?個(gè)可變的圖像Path句柄,該路徑?于保存繪圖信息
let pathRef = CGMutablePath()
// 獲取cell的size
// 第?個(gè)參數(shù),是整個(gè) cell 的 bounds, 第?個(gè)參數(shù)是距左右兩端的距離,第三個(gè)參數(shù)是距上下兩端的距離
let bounds = cell.bounds.insetBy(dx: 10, dy: 0)
// 判斷分組列表中的第??,每組section的第??,每組section的中間?
// CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius)
if indexPath.row == 0 {
// 初始起點(diǎn)為cell的左下?坐標(biāo)
pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
// 起始坐標(biāo)為左下?,設(shè)為p,
//(CGRectGetMinX(bounds), CGRectGetMinY(bounds))為左上?的點(diǎn),設(shè)為p1(x1,y1),
// (CGRectGetMidX(bounds), CGRectGetMinY(bounds))為頂部中點(diǎn)的點(diǎn),設(shè)為p2(x2,y2)。
//然后連接p1和p2為?條直線l1,連接初始點(diǎn)p到p1成?條直線l,則在兩條直線相交處繪制弧度為r的圓?。
pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius)
pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
// 終點(diǎn)坐標(biāo)為右下?坐標(biāo)點(diǎn),把繪圖信息都放到路徑中去,根據(jù)這些路徑就構(gòu)成了?塊區(qū)域了
pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
} else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1 {
pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)
pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
} else {
// 添加cell的rectangle信息到path中(不包括圓?)
// 假如?填充?,?這個(gè) CGPathAddRect(pathRef, nil, bounds)
// 假如只要邊框
pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
pathRef.addLine(to: CGPoint(x: bounds.midX, y: bounds.minY))
pathRef.move(to: CGPoint(x: bounds.maxX, y: bounds.minY))
pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
}
// 把已經(jīng)繪制好的可變圖像路徑賦值給圖層,然后圖層根據(jù)這圖像path進(jìn)?圖像渲染render layer.path = pathRef
backgroundLayer.path = pathRef
// 按照shape layer的path填充顏?,類似于渲染render
// layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor
layer.strokeColor = UIColor.black.cgColor
layer.fillColor = UIColor.clear.cgColor
// view??與cell?致
let roundView = UIView(frame: self.view.bounds)
// 添加?定義圓?后的圖層到roundView中
roundView.layer.insertSublayer(layer, at: 0)
roundView.backgroundColor = .clear
// cell的背景view
cell.backgroundView = roundView
// 以上?法存在缺陷當(dāng)點(diǎn)擊cell時(shí)還是出現(xiàn)cell?形效果,因此還需要添加以下?法
// 如果你 cell 已經(jīng)取消選中狀態(tài)的話,那以下?法是不需要的.
let selectedBackgroundView = UIView(frame: self.view.bounds)
backgroundLayer.fillColor = UIColor.cyan.cgColor
selectedBackgroundView.layer.insertSublayer(backgroundLayer, at: 0)
selectedBackgroundView.backgroundColor = .clear
cell.selectedBackgroundView = selectedBackgroundView
// 在使?上?代碼前需要把tableView默認(rèn)的分割線設(shè)置為None
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。