其实抓下京东app的首页,或者别的大型项目的首页啥的,一般会抓到多个页面相关的接口,例如轮播图的、数据列表、等等。
很多app会把所有接口请求完才展示数据,如果其中有一个错误就整个页面展示错误页面,我感觉这是对好的接口的一种浪费,所以我感觉应该做到 请求到一个就展示一个接口的数据;比如说轮播图请求成功就展示轮播图的 没请求成功的在相关的UI模块展示没成功的错误页面。
废话不说了,上代码;举例 加载框吧‘
先看使用
func loadAData() {
aView.showLoadingView()
RequestManager.request(path: "xxx/project-list", parameters: nil) { result in
aView.view.hiddenLoadingView()
switch result {
case .success(let data) :
self.updateUI(data: data)
let state:EmptyViewState = list.count == 0 ? .emptyData : .none
aView.showEmptyView(state: state) {
self.loadAData()
}
break
case .failure(let error):
aView.showEmptyView(state: error.toEmptyState()) {
self.loadAData()
}
aView.showToast(message: error.msg)
break
}
}
}
实现
//
// TKHUD.swift
// 模块开发
//
// Created by 天空吸引我 on 2020/6/11.
// Copyright © 2020 libtinker. All rights reserved.
//
import UIKit
class TKHUD:UIView {
lazy var messageLabel:UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 14.0)
label.textColor = UIColor.white
label.layer.masksToBounds = true
label.numberOfLines = 0
label.textAlignment = .center
return label
}()
lazy var contentView:UIView = {
let view = UIView()
view.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100)
view.backgroundColor = UIColor.init(red: 58/255.0, green: 59/255.0, blue: 73/255.0, alpha: 1.0).withAlphaComponent(0.5)
view.layer.cornerRadius = 4.0
self.addSubview(view)
return view
}()
lazy var activityIndicatorView:UIActivityIndicatorView = {
let activityIndicatorView = UIActivityIndicatorView()
activityIndicatorView.frame = CGRect.init(x: 10, y: 10, width: 80, height: 80)
activityIndicatorView.color = UIColor.white
if #available(iOS 13.0, *) {
activityIndicatorView.style = .large
} else {
activityIndicatorView.style = .whiteLarge
}
self.addSubview(activityIndicatorView)
return activityIndicatorView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = false
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func addLoadingView() {
self.contentView.addSubview(self.activityIndicatorView)
self.contentView.center = self.center
self.activityIndicatorView.startAnimating()
}
public func addToastView(message:String) {
self.contentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
let boundingRect = message.boundingRect(with: CGSize(width: self.bounds.size.width-90, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes:[NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14)], context: nil)
self.contentView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.size.width-60, height: boundingRect.height+20)
self.contentView.center = self.center;
self.messageLabel.text = message
self.contentView.addSubview(self.messageLabel)
self.messageLabel.frame = CGRect(x: (self.contentView.bounds.size.width-boundingRect.width)/2, y: (self.contentView.bounds.size.height-boundingRect.height)/2, width: boundingRect.width, height:boundingRect.height)
}
}
extension UIView {
public func showLoadingView(){
let loadingView = TKHUD(frame: self.bounds)
loadingView.addLoadingView()
self.addSubview(loadingView);
}
public func hiddenLoadingView(){
for subView in self.subviews {
if subView.classForCoder == TKHUD.classForCoder() {
subView.removeFromSuperview()
}
}
}
public func showToast(message:String,afterDelay:TimeInterval = 2.5) {
let toastView = TKHUD(frame: self.bounds)
toastView.addToastView(message: message)
self.addSubview(toastView)
if afterDelay>0 {
DispatchQueue.main.asyncAfter(deadline: .now() + afterDelay) {
toastView.removeFromSuperview()
}
}
}
}