美文网首页
iOS 绘制不同半径的圆角

iOS 绘制不同半径的圆角

作者: 前年的邂逅_Jerry | 来源:发表于2020-12-19 19:57 被阅读0次
    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
        }
    
    }
    

    相关文章

      网友评论

          本文标题:iOS 绘制不同半径的圆角

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