随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

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   懂事长qingzZ  阅读(3059)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示