//
|
// 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
|
}
|
}
|