美文网首页
ios列表设置圆角

ios列表设置圆角

作者: d4d5907268a9 | 来源:发表于2024-05-09 16:28 被阅读0次

    需要实现这样如下效果:


    image.png

    主要方法如下:

    func SetMutiBorderRoundingCorners(_ rectCorner: UIRectCorner, corner: CGFloat) {
            
            //        [UIRectCorner.bottomLeft, UIRectCorner.topRight]
            
            let maskPath = UIBezierPath.init(roundedRect: self.bounds,
                                             
                                             byRoundingCorners: rectCorner,
                                             
                                             cornerRadii: CGSize(width: corner, height: corner))
            
            let maskLayer = CAShapeLayer()
            
            maskLayer.frame = self.bounds
            
            maskLayer.path = maskPath.cgPath
            
            self.layer.mask = maskLayer
            
        }
        
    

    定义一个枚举

    enum RedMyPageCellUIPostion {
        case top         // 顶部
        case center      // 中间
        case bottom      // 底部
    }
    

    根据枚举值来设置cell的圆角

    var cellPosition :RedMyPageCellUIPostion = .center {
            didSet {
                switch cellPosition {
                case .top:
                    self.bgView.SetMutiBorderRoundingCorners([.topLeft, .topRight], corner: 10.0)
                case .center:
                    self.bgView.SetMutiBorderRoundingCorners([.allCorners], corner: 0.0)
                case .bottom:
                    self.bgView.SetMutiBorderRoundingCorners([.bottomLeft, .bottomRight], corner: 10.0)
                }
            }
        }
        
    

    在列表数据源处,根据数据源设置cell的枚举值

    var memuList :[[Model]] = [[Model]]()
    ......
    func numberOfSections(in tableView: UITableView) -> Int {
            return self.memuList.count;
        }
       
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let groupMenus = self.memuList[section];
            return groupMenus.count
        }
        
    ......
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = self.tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath) as! MyCell
            
            cell.accessoryType = .disclosureIndicator
            cell.selectionStyle = .none
            let groupMenus = self.memuList[indexPath.section];
            if 0 == indexPath.row {
                cell.cellPosition = .top
            } else if indexPath.row == (groupMenus.count - 1) {
                cell.cellPosition = .bottom
            } else {
                cell.cellPosition = .center
            }
                
            let model = groupMenus[indexPath.row]
    
            ......
    
            return cell
        }
    

    以上

    相关文章

      网友评论

          本文标题:ios列表设置圆角

          本文链接:https://www.haomeiwen.com/subject/kflsfjtx.html