iOS 绘制不同半径的圆角

2020-12-19  本文已影响0人  前年的邂逅_Jerry
class CMCoruseTicketRecordListView: UIView {
    let winScale = ConstUtil.winScale
    let fontScale = ConstUtil.fontScale
    private(set) lazy var bgLayer : CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.lineWidth = 3 * winScale
        layer.strokeColor = UIColor.red.cgColor
        layer.fillColor = UIColor.white.cgColor
        return layer
    }()
    init() {
        super.init(frame: .zero)
        self.layer.insertSublayer(bgLayer, at: 0)
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        setRadius(rect)
    }

    /// 设置圆角
    private func setRadius(_ rect : CGRect){
        let bigRadius = 15 * winScale
        let smallRadius = bigRadius / 2.0
        let bezierPath = UIBezierPath()
        /// 左上
        bezierPath.addArc(withCenter: CGPoint(x: bigRadius, y: bigRadius), radius: bigRadius, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: true)
        /// 右上
        bezierPath.addArc(withCenter: CGPoint(x: rect.width - bigRadius, y: bigRadius), radius: bigRadius, startAngle: -CGFloat.pi / 2, endAngle: 0, clockwise: true)
        /// 右下
        bezierPath.addArc(withCenter: CGPoint(x: rect.width - smallRadius, y: rect.height - smallRadius), radius: smallRadius, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: true)
        /// 左下
        bezierPath.addArc(withCenter: CGPoint(x: smallRadius, y: rect.height - smallRadius), radius: smallRadius, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi  , clockwise: true)
        bezierPath.addLine(to: CGPoint(x: 0, y: bigRadius))
        bgLayer.path = bezierPath.cgPath
    }

}
上一篇下一篇

猜你喜欢

热点阅读