f2cf74c7
yangbin
1.0.20(4)
|
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
//
// StepProgressView.swift
// Twear
//
// Created by yangbin on 2021/11/19.
import UIKit
//@IBDesignable
class StepProgressView: UIView {
@IBOutlet private weak var backView: UIView!
@IBOutlet private weak var thumbImageView: UIImageView!
@IBOutlet private weak var trackView: UIView!
@IBOutlet private weak var thumbLeftConstraint: NSLayoutConstraint!
@IBOutlet private weak var trackWidthConstraint: NSLayoutConstraint!
@IBInspectable
private var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
thumbImageView.layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
}
@IBInspectable
private var thumbIsHidden: Bool = false
var value: Int = 0 {
didSet {
refreshUI()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initFromNib()
// setupUI()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initFromNib()
// setupUI()
}
override class func awakeFromNib() {
super.awakeFromNib()
}
private func initFromNib() {
if let view = UINib(nibName: "StepProgressView", bundle: Bundle(for: type(of: self))).instantiate(withOwner: self, options: nil).first as? UIView {
view.frame = bounds
self.addSubview(view)
}
}
override func layoutSubviews() {
let width = self.thumbImageView.frame.size.width/2
thumbLeftConstraint.constant = -width
refreshUI()
}
private func refreshUI() {
thumbImageView.isHidden = thumbIsHidden
if thumbIsHidden {
trackWidthConstraint.constant = self.frame.width * CGFloat(value)/100
} else {
trackWidthConstraint.constant = cornerRadius + (self.frame.width - 2*cornerRadius) * CGFloat(value)/100
}
}
}
|