swift UITableViewCell 绘制边框加圆角

swift UITableViewCell 绘制边框加圆角,第1张

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    let cornerRadius: CGFloat = 10

    cell.backgroundColor = UIColor.clear

    let layer = CAShapeLayer()

    let pathRef = CGMutablePath()

    let bounds = cell.bounds.insetBy(dx: 10, dy: 0)

    if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1 {

       pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)

    } else if indexPath.row == 0 {

       pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
       
       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)
       
       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 {
        
       pathRef.addRect(bounds);
    }

    layer.path = pathRef

    //颜色修改

    layer.fillColor = UIColor.white.cgColor

//需要设置边框线加上这一行
// layer.strokeColor = UIColor.red.cgColor

    layer.lineWidth = 0.3

    let testView = UIView(frame: bounds)

    testView.layer.insertSublayer(layer, at: 0)

    cell.backgroundView = testView
}

lazy var tabelView:UITableView = {
    let tableView = UITableView(frame:view.bounds,style: UITableView.Style.grouped)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell1")
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell2")
    tableView.separatorStyle = .none
    tableView.estimatedRowHeight = 0.0;
    tableView.estimatedSectionHeaderHeight = 0.0;
    tableView.estimatedSectionFooterHeight = 0.0;
    return tableView
}()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/994557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存