美文网首页
2022-08-10

2022-08-10

作者: 小卒的梦 | 来源:发表于2022-08-10 01:11 被阅读0次

public struct SectionLayerShadowPosition : OptionSet {

    publicinit(rawValue:RawValue) {

        self.rawValue= rawValue

    }

    public let rawValue: Int

    public static var top = SectionLayerShadowPosition.init(rawValue: 1 << 0)

    public static var bottom = SectionLayerShadowPosition.init(rawValue: 1 << 1)

    public static var left = SectionLayerShadowPosition.init(rawValue: 1 << 2)

    public static var right = SectionLayerShadowPosition.init(rawValue: 1 << 3)

}

extension CAShapeLayer{

    func shadowPathPosition(

        _ position: SectionLayerShadowPosition,

        insetX:CGFloat=16,

        shadowWidth:CGFloat=3

    ) {

        letshapeLayer =self

        letbounds = shapeLayer.bounds.insetBy(dx: insetX, dy:0)

        letmaxX = bounds.maxX

        letmaxY = bounds.maxY

        letpath =UIBezierPath.init()

        //右边

        ifposition.contains(.right) {

            path.move(to: .init(x: maxX,y:0))

            path.addLine(to: .init(x: maxX + shadowWidth,y:0))

            path.addLine(to: .init(x: maxX + shadowWidth,y: maxY))

            path.addLine(to: .init(x: maxX,y: maxY))

        }

        //左边

        ifposition.contains(.left) {

            path.move(to: .init(x: insetX - shadowWidth,y:0))

            path.addLine(to: .init(x: insetX,y:0))

            path.addLine(to: .init(x: insetX,y: maxY))

            path.addLine(to: .init(x: insetX - shadowWidth,y: maxY))

        }

        //上边

        ifposition.contains(.top) {

            path.move(to: .init(x: insetX,y:0))

            path.addLine(to: .init(x: insetX,y: -shadowWidth))

            path.addLine(to: .init(x: maxX,y: -shadowWidth))

            path.addLine(to: .init(x: maxX,y:0))

        }

        //下边

        ifposition.contains(.bottom) {

            path.move(to: .init(x: insetX,y: maxY))

            path.addLine(to: .init(x: maxX,y: maxY))

            path.addLine(to: .init(x: maxX,y: maxY + shadowWidth))

            path.addLine(to: .init(x: insetX,y: maxY + shadowWidth))

        }

        shapeLayer.shadowPath= path.cgPath

        shapeLayer.shadowOffset= .zero

        shapeLayer.shadowRadius= shadowWidth

    }

}

extension UITableView{

    @discardableResult

    /// 设置section圆角

    /// - Parameters:

    ///  - cell: cell

    ///  - indexPath: indexPath

    ///  - cornerRadius: 圆角

    ///  - insetX: 偏移量x,默认无

    ///  - shadowWidth: 阴影宽度,nil表示不设置

    /// - Returns: BackgroundLayer

    public func setSectionCornerRadius(

        _cornerRadius:CGFloat=12,

        cell: UITableViewCell,

        indexPath:IndexPath,

        insetX:CGFloat=0,

        shadowWidth:CGFloat? =nil

    ) ->CAShapeLayer {

        lettableView =self

        //下面为设置圆角操作(通过遮罩实现)

        letsectionCount = tableView.numberOfRows(inSection: indexPath.section)

        letbounds = cell.bounds

        letlayerBounds = bounds.insetBy(dx: insetX, dy:0)

        letcornerRadii =CGSize.init(width: cornerRadius,height: cornerRadius)

        letbezierPath :UIBezierPath

        letexitBackgroundView = cell.backgroundView

        letbackgroundView = exitBackgroundView ??UIView()

        ifexitBackgroundView != backgroundView {

            cell.backgroundView= backgroundView

        }

        letexitLayer = backgroundView.layer.sublayers?.firstas?CAShapeLayer

        letshapeLayer = exitLayer ??CAShapeLayer()

        ifexitLayer != shapeLayer {

            backgroundView.layer.addSublayer(shapeLayer)

        }

        shapeLayer.frame= bounds

        /// 当前分区有多行数据时

        ifsectionCount >1{

            switchindexPath.row{

            case0:///如果是第一行,左上、右上角为圆角

                bezierPath =UIBezierPath(roundedRect: layerBounds,

                                          byRoundingCorners: [.topLeft, .topRight],

                                          cornerRadii: cornerRadii)

                ifletshadowWidth = shadowWidth {

                    shapeLayer.shadowPathPosition([.left, .top, .right],insetX: insetX,shadowWidth: shadowWidth)

                }else{

                    shapeLayer.path=nil

                }

            casesectionCount -1:///如果是最后一行,左下、右下角为圆角

                bezierPath =UIBezierPath(roundedRect: layerBounds,

                                          byRoundingCorners: [.bottomLeft, .bottomRight],

                                          cornerRadii: cornerRadii)

                ifletshadowWidth = shadowWidth {

                    shapeLayer.shadowPathPosition([.left, .bottom, .right],insetX: insetX,shadowWidth: shadowWidth)

                }else{

                    shapeLayer.path=nil

                }

            default:

                bezierPath = .init(rect: layerBounds)

                ifletshadowWidth = shadowWidth {

                    shapeLayer.shadowPathPosition([.left, .right],insetX: insetX,shadowWidth: shadowWidth)

                }else{

                    shapeLayer.path=nil

                }

            }

        }else{///当前分区只有一行行数据时

            /// 四个角都为圆角(同样设置偏移隐藏首、尾分隔线)

            bezierPath =UIBezierPath(roundedRect: layerBounds,cornerRadius: cornerRadius)

            ifletshadowWidth = shadowWidth {

                shapeLayer.shadowPathPosition([.top, .bottom, .left, .right],insetX: insetX,shadowWidth: shadowWidth)

            }else{

                shapeLayer.shadowPath=nil

            }

        }

        shapeLayer.path= bezierPath.cgPath

        returnshapeLayer

    }

}

  functableView(_tableView:UITableView,willDisplaycell:UITableViewCell,forRowAtindexPath:IndexPath) {

        letnormalLayer = tableView.setSectionCornerRadius(16,cell: cell,indexPath: indexPath,insetX:16,shadowWidth:10)

        //阴影

        normalLayer.shadowColor=UIColor.red.cgColor

        normalLayer.shadowOpacity=0.3

        ifindexPath.row%2==1{

            normalLayer.fillColor=UIColor.init(red:0.9,green:0.9,blue:0.9,alpha:1).cgColor

        }else{

            normalLayer.fillColor=UIColor.white.cgColor

        }

    }

相关文章

  • 2022-08-10

    Cancer Cell | 利用转座因子进行免疫治疗 原创huacishu图灵基因2022-08-10 08:33...

  • 2022-08-13 因子论实盘记录

    一、交易记录 A 账户:2022-08-10 每股 26.62 元卖出 5300 股太极集团。2022-08-10...

  • 2022-08-10

    Cell Reports | KRAS突变影响RNA“暗物质”可用于癌症早筛 图灵基因2022-08-10 08:...

  • 2022-08-10 唯恒教练客户 Demo

    2022-08-10 唯恒教练客户 Demo CICI记录,供内部拆解学习 1、如果我们有1个简短的1次教练的演示...

  • 写在妹妹高烧后

    幸福日志2022-08-10 周三 晴 烧了四天,最高39.9度,就这样反反复复,除了咳嗽,靠着自身的抗造力,妹妹...

  • 753故事续写

    原创 茹雪餐风 茹雪潇馨 2022-08-10 05:54 发表于浙江 某天,我再次踏上了753这趟从信阳开往上海...

  • 0282觉察日记|练习心力

    2022-08-10 北京 晴天 (前两天下雨了稍微降了下温)看了日落,19点在22层是最佳观景时间,完毕。 起床...

  • 瑟瑟发抖

    2022-08-10 周三 天气阴有小雨创城进入攻坚的日子,明后两天,国检人员进场,晚上加班排班,把已安排的人员固...

  • 顺势而为,不在乎影不影响别人

    顺势而为,不在乎影不影响别人 2022-08-10 曾经的飞猪理论,让无数人相信了可以飞的猪,更让人相信了“顺势而...

  • 2022-08-11

    论文写作指南书单(146本电子书目录) 科研写作研究所2022-08-10 21:00发表于山东 想要写出水平高、...

网友评论

      本文标题:2022-08-10

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