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
}
}