1. CAShapeLayer:适用于 动态显示 虚线, 通过控制添加虚线view的 ishidden 控制虚线的显示隐藏

 

 

    /// 绘制虚线
    /// - Parameters:
    ///   - lineView: 添加虚线的view
    ///   - strokeColor: 虚线颜色
    ///   - lineWidth: 虚线宽度
    ///   - lineLength: 每段虚线的长度
    ///   - lineSpacing: 每段虚线的间隔
    private func drawDashLine(lineView:UIView,
                              strokeColor: UIColor,
                              lineWidth: CGFloat = 0.5,
                              lineLength: Int = 4,
                              lineSpacing: Int = 4) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.bounds = lineView.bounds
        shapeLayer.position = CGPoint(x: lineView.width/2, y: lineView.height/2)
        shapeLayer.fillColor = UIColor.blue.cgColor
        shapeLayer.strokeColor = strokeColor.cgColor
        
        shapeLayer.lineWidth = lineWidth
        shapeLayer.lineJoin = CAShapeLayerLineJoin.round
        shapeLayer.lineDashPhase = 0
        //每一段虚线长度 和 每两段虚线之间的间隔
        shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
        
        let path = CGMutablePath()
    ///起点 path.move(to: CGPoint(x: lineView.width
/2, y: 0))

    ///终点
/// 横向 y = 0 // path.addLine(to: CGPoint(x: 0, y: 0)) ///纵向 Y = view 的height path.addLine(to: CGPoint(x: lineView.frame.width/2, y: lineView.frame.height)) shapeLayer.path = path lineView.layer.addSublayer(shapeLayer) }

 

2.UIGraphicsGetCurrentContext绘制 需要在draw 方法使用

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        self.drawBgView(context: context, rect: rect)
        // 设置左右半圆
        let radius: CGFloat = 6
        let centerY: CGFloat = rect.height - 49
        self.drawCircle(context: context, rect: rect, centerY: centerY, radius: radius)
        // 设置水平方向半圆中心虚线
        let h_startPoint: CGPoint = CGPoint(x: radius + 8, y: centerY )
        let h_endPoint: CGPoint = CGPoint(x: rect.width - (radius + 8), y: centerY)
        self.drawDashLin(startPoint: h_startPoint, endPoint: h_endPoint, context: context)
        if isPriceView == false {
            /// 优惠券与迎新券分割虚线
            let s_stasrtPoint: CGPoint = CGPoint(x: 16, y: 70 )
            let s_endPoint: CGPoint = CGPoint(x: rect.width - 16, y: 70)
            self.drawDashLin(startPoint: s_stasrtPoint, endPoint: s_endPoint, context: context)
        }
    }
    private func addShadow() {
        self.layer.shadowColor = UIColor(hexString: "000000").withAlphaComponent(0.1).cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: 0)
        self.layer.shadowOpacity = 1
        self.layer.shadowRadius = 5
    }

    /// 绘制阴影view
    private func drawBgView(context: CGContext , rect: CGRect) {
        context.saveGState()
        // 设置填充色
        UIColor.white.set()
        context.addPath(UIBezierPath(roundedRect: rect, cornerRadius: 10).cgPath)
        context.fillPath()
        context.restoreGState()
    }

    /// 绘制左右半圆
    private func drawCircle(context: CGContext , rect: CGRect , centerY: CGFloat , radius: CGFloat) {
        //1.设置圆心位置
        let leftCenter: CGPoint = CGPoint(x: 0, y: centerY)
        let rightCenter: CGPoint = CGPoint(x: rect.width, y: centerY)
        context.saveGState()
        //2.设置路径
        let leftPath = UIBezierPath(arcCenter: leftCenter, radius: radius, startAngle: -CGFloat.pi/2, endAngle: CGFloat.pi * 1.5, clockwise: true)
        let rightPath = UIBezierPath(arcCenter: rightCenter, radius: radius, startAngle: CGFloat.pi/2, endAngle: CGFloat.pi * 1.5, clockwise: true)
        context.setBlendMode(.clear)
        context.setLineWidth(1)
        context.addPath(leftPath.cgPath)
        context.addPath(rightPath.cgPath)
        context.fillPath()
        context.restoreGState()
    }

    /// 绘制虚线
    private func drawDashLin(startPoint: CGPoint , endPoint: CGPoint , context: CGContext) {
        context.saveGState()
        let path = CGMutablePath()
        path.move(to: startPoint)
        path.addLine(to: endPoint)
        context.addPath(path)
        context.setStrokeColor(UIColor(hexString: "CDCDCD").cgColor)
        context.setLineWidth(1)
        context.setLineDash(phase: 0, lengths: [3 , 3])
        context.strokePath()
        context.restoreGState()
    }

 

posted on 2020-08-20 11:12  懂事长qingzZ  阅读(2855)  评论(0编辑  收藏  举报