Skip to content

Commit 56213a9

Browse files
committed
Create DownloadButton.swift
1 parent 38a8265 commit 56213a9

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// DownloadButton.swift
3+
// FXViewKit
4+
//
5+
// Created by aria on 2023/2/19.
6+
//
7+
8+
import Foundation
9+
10+
public class DownloadButton: UIButton {
11+
12+
public enum DownloadButtonState {
13+
case pending
14+
case downloading
15+
case downloaded
16+
}
17+
18+
public var downloadState: DownloadButtonState = .pending {
19+
didSet {
20+
updateState()
21+
}
22+
}
23+
24+
public let startButton = UIButton()
25+
public let indicator = UIActivityIndicatorView()
26+
public let downloadedButton = UIButton()
27+
28+
public override init(frame: CGRect) {
29+
super.init(frame: frame)
30+
31+
startButton.isUserInteractionEnabled = false
32+
indicator.isUserInteractionEnabled = false
33+
downloadedButton.isUserInteractionEnabled = false
34+
35+
addSubview(startButton)
36+
addSubview(indicator)
37+
addSubview(downloadedButton)
38+
39+
updateState()
40+
}
41+
42+
required init?(coder: NSCoder) {
43+
fatalError("init(coder:) has not been implemented")
44+
}
45+
46+
public override func layoutSubviews() {
47+
super.layoutSubviews()
48+
startButton.frame = bounds
49+
indicator.frame = bounds
50+
downloadedButton.frame = bounds
51+
}
52+
53+
private func updateState() {
54+
isEnabled = downloadState != .downloading
55+
startButton.isHidden = downloadState != .pending
56+
indicator.isHidden = downloadState != .downloading
57+
58+
if downloadState != .downloading {
59+
indicator.stopAnimating()
60+
} else {
61+
indicator.startAnimating()
62+
}
63+
64+
downloadedButton.isHidden = downloadState != .downloaded
65+
}
66+
67+
}

0 commit comments

Comments
 (0)