Swift Control DashLineView(虚线)

一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。

Swift Control DashLineView(虚线) 下载链接

文章目录

效果

使用

c 复制代码
let separatorLine = DashLineView()
separatorLine.lineColor = .lightGray
separatorLine.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(separatorLine)
separatorLine.snp.makeConstraints { make in
    make.center.equalToSuperview()
    make.width.equalToSuperview()
    make.height.equalTo(1)
    
}

封装代码

bash 复制代码
import UIKit

class DashLineView: UIView {

    var lineColor: UIColor = .gray {
        didSet {
            dashLayer.strokeColor = lineColor.cgColor
        }
    }

    private let dashLayer = CAShapeLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setup() {
        dashLayer.strokeColor = lineColor.cgColor
        dashLayer.fillColor = UIColor.clear.cgColor
        
        // 虚线长度2,间隔2
        dashLayer.lineDashPattern = [4, 4]
        dashLayer.lineWidth = 1
        
        layer.addSublayer(dashLayer)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: bounds.height / 2))
        path.addLine(to: CGPoint(x: bounds.width, y: bounds.height / 2))

        dashLayer.path = path.cgPath
        dashLayer.frame = bounds
    }
}
相关推荐
weixin_4407305028 分钟前
字符编码笔记-utf-8 、unicode、gbk,ascii编码格式
开发语言·笔记
励志不掉头发的内向程序员1 小时前
【从零写一个CAD 03】三个 double 值得单独一个类吗:把视图变换抽成 View
开发语言·c++·qt·学习·系统架构
PHP实战开发录2 小时前
PHP接口大整数为什么变了
开发语言·php
临床数据科学和人工智能兴趣组5 小时前
在R语言中, 使用 as.factor() 函数转换数值型变量
开发语言·r语言
andyweike8 小时前
php笔记
开发语言·笔记·php
传奇开心果编程9 小时前
【SwiftUI娓娓道来】第3课:让界面活起来——交互与动画指南
学习·macos·ui·ios·swiftui·swift
Circ.9 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
可乐鸡翅yeah_10 小时前
新手梳理:M3U8 线上问题,哪些是前端锅,哪些是后端锅
前端·ios·音视频·实时音视频·m3u8·音视频在线播放
现任明教教主~11 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou14113611 小时前
SpringSecurity_02_授权与高级功能
开发语言·python