zoukankan      html  css  js  c++  java
  • swift 画虚线

    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( 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()
        }
  • 相关阅读:
    [置顶] cAdvisor、InfluxDB、Grafana搭建Docker1.12性能监控平台
    15 个 Docker 技巧和提示
    Docker资源管理探秘:Docker背后的内核Cgroups机制
    SVN通过域名连不上服务器地址(svn: E175002: OPTIONS request failed on '/svn/yx-SVN-Server' Connection refused: connect)
    图片点击放大功能
    jquery-选择checkbox的多种策略
    HTML-input标签需设置的属性
    MyBatis-配置缓存
    SpringMVC redirect乱码问题
    Mysql-左连接查询条件失效的解决办法
  • 原文地址:https://www.cnblogs.com/qingzZ/p/13533963.html
Copyright © 2011-2022 走看看