Swift:給UITableView的section添加陰影

話不多少,上代碼:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // 在該方法下對每個section設置陰影
        // 圓角角度
        let radius = kMarginMid
        // 設置cell 背景色為透明
        cell.backgroundColor = .clear
        // 創(chuàng)建layer
        let normalLayer = CAShapeLayer()
        // 獲取顯示區(qū)域大小
        let bounds = cell.bounds.insetBy(dx: kMarginMid, dy: 0)
        // cell的backgroundView
        let normalBgView = UIView(frame: bounds)
        // 獲取每組行數(shù)
        let rowNum = tableView.numberOfRows(inSection: indexPath.section)
        // 貝塞爾曲線
        var bezierPath = UIBezierPath()
        if rowNum == 1 {
            // 一組只有一行(四個角全部為圓角)
            bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: radius, height: radius))
            normalBgView.clipsToBounds = false
        } else {
            normalBgView.clipsToBounds = true
            if indexPath.row == 0 {
                let top = indexPath.section == 0 ? kMarginMin : RW(5)
                let rect = bounds.inset(by: UIEdgeInsets(top: top, left: 0, bottom: 0, right: 0))
                // 每組第一行(添加左上和右上的圓角)
                bezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: radius, height: radius))
            } else if indexPath.row == rowNum - 1 {
                let rect = bounds.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: RW(5), right: 0))
                // 每組最后一行(添加左下和右下的圓角)
                bezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: radius, height: radius))
            } else {
                // 每組不是首位的行不設置圓角
                bezierPath = UIBezierPath(rect: bounds)
            }
        }
        // 陰影
        normalLayer.shadowColor   = UIColor.black.cgColor;
        normalLayer.shadowOpacity = 0.15
        normalLayer.shadowOffset  = CGSize(width: 0, height: 1)
        normalLayer.path          = bezierPath.cgPath
        normalLayer.shadowPath    = bezierPath.cgPath
        // 設置填充顏色
        normalLayer.fillColor        = UIColor.white.cgColor
        // 添加圖層到nomarBgView中
        normalBgView.backgroundColor = UIColor.clear
        // 如果是第一個分區(qū),那么需要再額外加一層
        if indexPath.section == 0 && indexPath.row == 0 {
            normalBgView.backgroundColor = APP_MainColor
            let basicLayer  = CAShapeLayer()
            basicLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: RW(22.5), height: RW(22.5))).cgPath
            basicLayer.fillColor = UIColor.white.cgColor
//            normalBgView.layer.insertSublayer(basicLayer, at: 0)
            basicLayer.addSublayer(normalLayer)
            normalBgView.layer.insertSublayer(basicLayer, at: 0)
        } else {
            normalBgView.layer.insertSublayer(normalLayer, at: 0)
        }
        cell.backgroundView          = normalBgView
    }
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容