swift-可移动进度条

import UIKit
//140  height 4  圆角就是 2  #EEEEEE 大的背景  小的背景#6CA9FF
class YJProgressView: UIView {
    private let indicaterView = UIView()
    private var wholePageNum = 0
    private var indivaterIndex = 0
    //进度条畅读
    var progressViewH: CGFloat {
        get {
            return self.frame.size.height
        }
    }
    
    var progressViewW: CGFloat {
        get {
            return self.frame.size.width
        }
    }

    
    //进度条宽度
    var indicaterW: CGFloat {
        get {
            return self.frame.size.width / CGFloat(wholePageNum)
        }
    }
    
    //进度条圆角
    var corner: CGFloat {
        get {
            return self.frame.size.height / 2.0
        }
        
        set {
            
        }
    }

    init(frame: CGRect, wholePageCount: Int) {
        super.init(frame: frame)
        backgroundColor = .white //#EEEEEE
        wholePageNum = wholePageCount //总页数
        createContentViews()

    }
    
    //更新进度信息
    func updateIndicater(sliderToIndex: Int, wholePageCount: Int) {
        wholePageNum = wholePageCount //总页数
        if sliderToIndex <= wholePageNum {
            indivaterIndex = sliderToIndex
            let sliderX = CGFloat(sliderToIndex) * indicaterW
            indicaterView.snp.updateConstraints { (make) in
                make.left.equalTo(sliderX)
                make.width.equalTo(indicaterW)
            }
        }
    }
    
    //更新进度条的x值
    func updateIndicater(sliderToIndex: Int) {
        if sliderToIndex <= wholePageNum {
            indivaterIndex = sliderToIndex
            let sliderX = CGFloat(sliderToIndex) * indicaterW
            indicaterView.snp.updateConstraints { (make) in
                make.left.equalTo(sliderX)
                make.width.equalTo(indicaterW)
            }
        }
    }
    
    private func createContentViews() {
        addSubview(indicaterView)
        indicaterView.backgroundColor = .green //#6CA9FF
        indicaterView.layer.cornerRadius = corner
        indicaterView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.top.bottom.equalToSuperview()
            make.width.equalTo(indicaterW)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updateIndicater(sliderToIndex: indivaterIndex)
    }
}

使用方式:

func testIndicaterView() {
//        let xx = CGRect(x: 20, y: 100, width: 140, height: 4)
        let prg1 = YJProgressView.init(frame: CGRect.zero, wholePageCount: 11)
        prg1.backgroundColor = .black
        view.addSubview(prg1)
        prg1.snp.makeConstraints { (make) in
            make.width.equalTo(140)
            make.height.equalTo(4)
            make.center.equalToSuperview()
        }
        prg = prg1
    }

prg?.updateIndicater(sliderToIndex: curr)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值