-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSTShimmerLabel.swift
More file actions
150 lines (129 loc) · 4.43 KB
/
Copy pathSTShimmerLabel.swift
File metadata and controls
150 lines (129 loc) · 4.43 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// STShimmerLabel.swift
// Bajoseek
//
// Created by 寒江孤影 on 2026/2/3.
//
import UIKit
public class STShimmerLabel: STLabel {
private let gradientLayer = CAGradientLayer()
private let textMaskLayer = CATextLayer()
private let shimmerKey = "st.shimmer"
var shimmerDuration: CFTimeInterval = 1.4
var highlightWidth: CGFloat = 0.25 {
didSet { self.updateGradient() }
}
override public init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
self.setup()
}
override public func layoutSubviews() {
super.layoutSubviews()
self.updateFrames()
}
override public var text: String? {
didSet { self.updateTextMask() }
}
override public var attributedText: NSAttributedString? {
didSet { self.updateTextMask() }
}
override public var font: UIFont! {
didSet { self.updateTextMask() }
}
override public var textAlignment: NSTextAlignment {
didSet { self.updateTextMask() }
}
override public var numberOfLines: Int {
didSet { self.updateTextMask() }
}
private func setup() {
self.textColor = UIColor.label.withAlphaComponent(0.6)
self.gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
self.gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
self.layer.addSublayer(self.gradientLayer)
self.gradientLayer.isHidden = true
self.gradientLayer.mask = self.textMaskLayer
self.textMaskLayer.contentsScale = UIScreen.main.scale
self.textMaskLayer.isWrapped = true
self.textMaskLayer.truncationMode = .none
self.updateGradient()
self.updateTextMask()
}
private func updateFrames() {
CATransaction.begin()
CATransaction.setDisableActions(true)
self.gradientLayer.frame = bounds
self.textMaskLayer.frame = bounds
CATransaction.commit()
}
private func updateGradient() {
let highlight = self.highlightWidth
self.gradientLayer.colors = [
UIColor.clear.cgColor,
UIColor.white.withAlphaComponent(0.9).cgColor,
UIColor.clear.cgColor
]
self.gradientLayer.locations = [
NSNumber(value: 0.5 - highlight),
NSNumber(value: 0.5),
NSNumber(value: 0.5 + highlight)
]
}
private func updateTextMask() {
if let attr = self.attributedText {
self.textMaskLayer.string = attr
} else {
self.textMaskLayer.string = self.text
}
self.textMaskLayer.font = self.font
self.textMaskLayer.fontSize = self.font.pointSize
self.textMaskLayer.alignmentMode = self.alignmentMode(from: self.textAlignment)
}
private func alignmentMode(from alignment: NSTextAlignment) -> CATextLayerAlignmentMode {
switch alignment {
case .left: return .left
case .right: return .right
case .center: return .center
case .justified: return .justified
default: return .natural
}
}
public func startShimmer() {
guard self.gradientLayer.animation(forKey: self.shimmerKey) == nil else { return }
self.gradientLayer.isHidden = false
self.gradientLayer.add(self.makeAnimation(), forKey: self.shimmerKey)
}
public func stopShimmer(_ animated: Bool = true) {
guard self.gradientLayer.animation(forKey: self.shimmerKey) != nil else { return }
let finish = {
self.gradientLayer.removeAnimation(forKey: self.shimmerKey)
self.gradientLayer.isHidden = true
}
if animated {
UIView.animate(withDuration: 0.18, animations: {
self.alpha = 0
}, completion: { _ in
self.alpha = 1
finish()
})
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
finish()
}
}
}
private func makeAnimation() -> CABasicAnimation {
let anim = CABasicAnimation(keyPath: "locations")
anim.fromValue = [-1.0, -0.5, 0.0]
anim.toValue = [1.0, 1.5, 2.0]
anim.duration = self.shimmerDuration
anim.repeatCount = .infinity
anim.timingFunction = CAMediaTimingFunction(name: .linear)
anim.isRemovedOnCompletion = false
return anim
}
}