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)