IOS 15 实现Toast和小菊花Loading提示

本文主要是实现toast和loading两种提示功能,例如:登陆时参数不正确提示,toast提示后会自动隐藏。加载提示:不会自动隐藏,常用于网络请求,上传等。

IOS 15 实现Toast和小菊花Loading提示_第1张图片

添加依赖

#提示框架
#https://github.com/jdg/MBProgressHUD
pod 'MBProgressHUD'

还不了解如何使用 CocoaPods 管理依赖的,建议先看前面的文章:IOS 01 CocoaPods 安装与使用

IOS 15 实现Toast和小菊花Loading提示_第2张图片

添加完依赖后,看一下Pods文件夹里面是否添加成功。 

导入头文件

由于项目是由Swift写的,而MBProgressHUD是OC的,所以要在桥接文件中导入

//提示框架
#import 

定义SuperToast

//
//  SuperToast.swift
//  提示
//
//  Created by smile on 2022/7/6.
//

import UIKit

class SuperToast {
    static var hud:MBProgressHUD?
    
    /// 显示提示
    /// - Parameter title: <#title description#>
    static func show(title:String) {
        let hud = MBProgressHUD.showAdded(to: AppDelegate.shared.window!.rootViewController!.view, animated: true)
        hud.mode = .text
        
        //背景颜色
        hud.bezelView.style = .solidColor
        hud.bezelView.backgroundColor = .black
        
        //标题文字颜色
        hud.label.textColor = .colorLightWhite
        hud.label.font = UIFont.boldSystemFont(ofSize: 16)
        hud.label.numberOfLines = 0
        
        hud.label.text = title
        
        let offsetY = -hud.frame.height/CGFloat(2)+80
        
        //显示到屏幕顶部
        //因为显示到中心有点遮挡内容
        hud.offset = CGPoint(x: 0, y: offsetY)
        
        hud.removeFromSuperViewOnHide = true
        
        hud.hide(animated: true, afterDelay: 1.5)
    }
    
    /// 加载提示
    /// - Parameter title: <#title description#>
    static func showLoading(title:String = R.string.localizable.superLoading()) {
        //菊花颜色
        UIActivityIndicatorView.appearance(whenContainedInInstancesOf:
            [MBProgressHUD.self]).color = .white
        
        if SuperToast.hud == nil {
            SuperToast.hud = MBProgressHUD.showAdded(to: AppDelegate.shared.window!.rootViewController!.view, animated: true)
            SuperToast.hud!.mode = .indeterminate
            
            //最小尺寸
            SuperToast.hud!.minSize = CGSize(width: 120, height: 120)
            
            //背景半透明
            SuperToast.hud!.backgroundView.style = .solidColor
            SuperToast.hud!.backgroundView.color = UIColor(white: 0, alpha: 0.5)
            
            //背景颜色
            SuperToast.hud!.bezelView.style = .solidColor
            SuperToast.hud!.bezelView.backgroundColor = .black
            
            //标题文字颜色
            SuperToast.hud!.label.textColor = .colorLightWhite
            SuperToast.hud!.label.font = UIFont.boldSystemFont(ofSize: TEXT_LARGE)
            
            //显示对话框
            SuperToast.hud!.show(animated: true)
        }
        
        //设置对话框文字
        SuperToast.hud!.label.text = title
        
        //详细文字
//        SuperToast.hud!.detailsLabel.text = "请耐心等待"
    }
    
    /// 隐藏提示
    static func hideLoading() {
        if let r = SuperToast.hud {
            r.hide(animated: true)
            SuperToast.hud = nil
        }
    }
}

使用

SuperToast.show(title: "\(data.data.data?[0].title!)")

SuperToast.showLoading()

你可能感兴趣的:(IOS,ios)