宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-11 4356615a9252a987a62469331b1fcf91c102e24c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
//  YYCollectionViewController.swift
//  EDriverProject
//
//  Created by alvin_y on 2020/3/11.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import DZNEmptyDataSet
 
class YYCollectionViewController: YYViewController {
    
    enum EmptyType {
        case none
        case empty(image: UIImage?, title: String?)
        case refresh
        case error(errorDescription: String?, recoverySuggestion: String?)
    }
    
    var collectionView: UICollectionView!
    
    var emptyImage: UIImage?
    var emptyTitle: String?
    var emptyTitleColor: UIColor?
    var emptyTitleFont: UIFont?
    var emptyDescription: String?
    var emptyButonTitle: String?
    var emptyType: EmptyType = .none {
        didSet {
            
            switch emptyType {
                
            case .none:
                emptyImage = nil
                emptyTitle = nil
                emptyDescription = nil
                emptyButonTitle = nil
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            case .empty(let image, let title):
                emptyImage = image
                emptyTitle = title
                emptyDescription = nil
                emptyButonTitle = nil
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            case .refresh:
                emptyImage = #imageLiteral(resourceName: "load")
                emptyTitle = "正在载入..."
                emptyDescription = nil
                emptyButonTitle = nil
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            case .error(let errorDescription, let recoverySuggestion):
                emptyImage = nil
                emptyTitle = errorDescription
                emptyDescription = recoverySuggestion
                emptyButonTitle = "重试"
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            }
            
            collectionView.reloadEmptyDataSet()
        }
    }
    
    var retryHandler: (() -> Void)?
    
    var viewLayout = UICollectionViewFlowLayout()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        collectionView.reloadEmptyDataSet()
    }
    
    override func setupViews() {
        super.setupViews()
        viewLayout.minimumLineSpacing = 0
        viewLayout.minimumInteritemSpacing = 0
        collectionView = UICollectionView.init(frame: view.bounds, collectionViewLayout: viewLayout)
        collectionView.backgroundColor = #colorLiteral(red: 0.9647058824, green: 0.9647058824, blue: 0.9647058824, alpha: 1)
        collectionView.emptyDataSetSource = self
        collectionView.emptyDataSetDelegate = self
        view.addSubview(collectionView)
        
        emptyType = .empty(image: nil, title: "暂无数据")
    }
    
    
}
extension YYCollectionViewController: DZNEmptyDataSetSource {
    
    func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return -((navigationController?.navigationBar.bounds.height ?? 0) + UIApplication.shared.statusBarFrame.height) + 20
    }
//    func offset(forEmptyDataSet scrollView: UIScrollView!) -> CGPoint {
//        let y = -((navigationController?.navigationBar.bounds.height ?? 0) + UIApplication.shared.statusBarFrame.height) + 20
//        return CGPoint.init(x: 0, y: y)
//    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        
        return emptyImage ?? UIImage()
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        switch emptyType {
            
        case .refresh:
            return emptyTitle?.makeAttributedString(color: emptyTitleColor!, font: emptyTitleFont!)
            
        default:
            return emptyTitle?.makeAttributedString(color: emptyTitleColor!, font: emptyTitleFont!)
        }
    }
    
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        return emptyDescription?.makeAttributedString(color: #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1), font: UIFont.systemFont(ofSize: 14))
    }
    
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControl.State) -> NSAttributedString! {
        return emptyButonTitle?.makeAttributedString(color: (state == .normal) ? #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1) : #colorLiteral(red: 0.9215686275, green: 0.9215686275, blue: 0.9215686275, alpha: 1), font: UIFont.systemFont(ofSize: 14))
    }
    
    func buttonBackgroundImage(forEmptyDataSet scrollView: UIScrollView!, for state: UIControl.State) -> UIImage! {
        
        let margin = (UIScreen.main.bounds.width - 32 - 80) / 2
        
        let capInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        let rectInsets = UIEdgeInsets(top: -19.0, left: -margin, bottom: -19.0, right: -margin)
        let image = emptyButonTitle == nil ? nil : ((state == .normal) ? #imageLiteral(resourceName: "button_background_icloud_normal") : #imageLiteral(resourceName: "button_background_icloud_highlight"))
        
        return image?.resizableImage(withCapInsets: capInsets, resizingMode: .stretch).withAlignmentRectInsets(rectInsets)
        
    }
    
    func imageAnimation(forEmptyDataSet scrollView: UIScrollView!) -> CAAnimation! {
        
        let animation = CABasicAnimation(keyPath: "transform")
        animation.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
        animation.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(.pi / 2, 0.0, 0.0, 1.0))
        animation.duration = 0.25
        animation.isCumulative = true
        animation.repeatCount = MAXFLOAT
        
        return animation;
    }
    
}
 
extension YYCollectionViewController: DZNEmptyDataSetDelegate {
    
    func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView!) -> Bool {
        switch emptyType {
            
        case .refresh:
            return true
            
        default:
            return false
        }
    }
    
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap button: UIButton!) {
        switch emptyType {
        case .error(_, _):
            retryHandler?()
        default:
            break
        }
    }
    
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
}