swift——虚线边框

2023-07-31  本文已影响0人  不洗澡的鱼

给View设置虚线边框

/**
    - width:虚线线宽
    - length:虚线长度
    - space:虚线空隙长度
    - cornerRadius:圆角
    - color:虚线颜色
*/
 func drawLayerDashedLine(width: CGFloat, length: CGFloat, space: CGFloat, cornerRadius: CGFloat, color: UIColor) {
        self.layer.cornerRadius = cornerRadius
        let borderLayer =  CAShapeLayer()
        borderLayer.bounds = self.bounds 
        borderLayer.position = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
        borderLayer.path = UIBezierPath(roundedRect: borderLayer.bounds, cornerRadius: cornerRadius).cgPath
        borderLayer.lineWidth = width / UIScreen.main.scale
        borderLayer.lineDashPattern = [length,space] as? [NSNumber] 
        borderLayer.lineDashPhase = 0.1
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = color.cgColor
        self.layer.addSublayer(borderLayer)
    }

更改内容边框没有刷新时,调用

self.view.setNeedsLayout()
self.view.layoutIfNeeded()
上一篇 下一篇

猜你喜欢

热点阅读