iOS swift5 弹出提示文字(停留1~2s)XHToastSwift

news/2024/7/20 21:27:21 标签: ios

CoderZhuXH/XHToastSwift - github

//
//  XHToast.swift
//  XHToastSwiftExample
//
//  Created by xiaohui on 16/8/12.
//  Copyright © 2016年 CoderZhuXH. All rights reserved.
//  代码地址:https://github.com/CoderZhuXH/XHToastSwift


import UIKit

/**
 *  Toast默认停留时间 1.2
 */
private let ToastDispalyDuration:CGFloat = 1.2
/**
 *  Toast到顶端/底端默认距离
 */
private let ToastSpace:CGFloat = 100.0
/**
 *  Toast背景颜色
 */
private let ToastBackgroundColor = UIColor(red:0.2,green:0.2,blue:0.2,alpha:0.75)

//在window上显示
extension XHToast
{
    //MARK:-中间显示
    
    /**
     中间显示
     
     - parameter text: 文字
     */
    public class func showCenterWithText(_ text: String) {
        
        XHToast.showCenterWithText(text, duration:ToastDispalyDuration)
    }
    
    /**
     中间显示+自定义时间
     
     - parameter text:     文字
     - parameter duration: 自定义停留时间
     */
    public class func showCenterWithText(_ text:String,duration:CGFloat) {
        let toast = XHToast(text: text)
        toast.duration = duration
        toast.showIn(UIWindow.window())
    }

    // MARK:-上方显示
    
    /**
     上方显示
     
     - parameter text: 文字
     */
    public class func showTopWithText(_ text:String) {
        XHToast.showTopWithText(text, topOffset:ToastSpace, duration:ToastDispalyDuration)
    }
    
    /**
     上方显示+自定义停留时间
     
     - parameter text:     文字
     - parameter duration: 自定义停留时间
     */
    public class func showTopWithText(_ text:String, duration:CGFloat) {
        XHToast.showTopWithText(text, topOffset:ToastSpace, duration:duration)
    }
    
    /**
     上方显示+自定义到顶部距离
     
     - parameter text:      文字
     - parameter topOffset: 自定义到顶部距离
     */
    public class func showTopWithText(_ text:String,topOffset:CGFloat) {
        XHToast.showTopWithText(text, topOffset:topOffset, duration:ToastDispalyDuration)
    }
    
    /**
     上方显示+自定义到顶部距离+自定义停留时间
     
     - parameter text:      文字
     - parameter topOffset: 自定义到顶部距离
     - parameter duration:  自定义停留时间
     */
    public class func showTopWithText(_ text:String, topOffset:CGFloat,duration:CGFloat) {
        let toast = XHToast(text: text)
        toast.duration = duration
        toast.showIn(UIWindow.window(), topOffset: topOffset)
    }
    
    // MARK:-下方显示
    
    /**
     下方显示
     
     - parameter text: 文字
     */
    public class func showBottomWithText(_ text:String) {
        XHToast.showBottomWithText(text, bottomOffset:ToastSpace, duration:ToastDispalyDuration)
    }
    
    /**
     下方显示+自定义停留时间
     
     - parameter text:     文字
     - parameter duration: 自定义停留时间
     */
    public class func showBottomWithText(_ text:String,duration:CGFloat) {
        XHToast.showBottomWithText(text, bottomOffset:ToastSpace, duration:duration)
    }
    
    /**
     下方显示+自定义到底部距离
     
     - parameter text:         文字
     - parameter bottomOffset: 自定义到底部距离
     */
    public class func showBottomWithText(_ text:String,bottomOffset:CGFloat) {
        XHToast.showBottomWithText(text, bottomOffset:bottomOffset, duration:ToastDispalyDuration)
    }
    
    /**
     下方显示+自定义到底部距离+自定义停留时间
     
     - parameter text:         文字
     - parameter bottomOffset: 自定义到底部距离
     - parameter duration:     自定义停留时间
     */
    public class func showBottomWithText(_ text:String,bottomOffset:CGFloat,duration:CGFloat) {
        let toast: XHToast = XHToast(text: text)
        toast.duration = duration
        toast.showIn(UIWindow.window(), bottomOffset: bottomOffset)
    }
    
}

//在view上显示
extension UIView
{
    // MARK:- 中间显示
    
    /// 中间显示
    ///
    /// - Parameter text: 文字
    public func showXHToastCenterWithText(_ text:String){
        
        self.showXHToastCenterWithText(text, duration: ToastDispalyDuration)
    
    }
    
    
    /// 中间显示+自定义停留时间
    ///
    /// - Parameters:
    ///   - text: 文字
    ///   - duration: 自定义停留时间
    public func showXHToastCenterWithText(_ text:String , duration:CGFloat){
    
        let toast: XHToast = XHToast(text: text)
        toast.duration = duration
        toast.showIn(self)
    
    }
    
    
    // MARK:-上方显示
    
    /// 上方显示
    ///
    /// - Parameter text: 文字
    public func showXHToastTopWithText(_ text:String){
    
       self.showXHToastTopWithText(text, topOffset: ToastSpace, duration: ToastDispalyDuration)
    }
    
    
    /// 上方显示+自定义停留时间
    ///
    /// - Parameters:
    ///   - text: 文字
    ///   - duration: 自定义停留时间
    public func showXHToastTopWithText(_ text:String,  duration:CGFloat){
    
      self.showXHToastTopWithText(text, topOffset: ToastSpace, duration: duration)
    
    }
    
    
    /// 上方显示+自定义到顶部距离
    ///
    /// - Parameters:
    ///   - text: 文字
    ///   - topOffset: 自定义到顶部距离
    public func showXHToastTopWithText(_ text:String,topOffset:CGFloat){
    
        self.showXHToastTopWithText(text, topOffset: topOffset, duration: ToastDispalyDuration)
    
    }
    
    
    /// 上方显示+自定义到顶部距离+自定义停留时间
    ///
    /// - Parameters:
    ///   - text: 文字
    ///   - topOffset: 自定义到顶部距离
    ///   - duration: 自定义停留时间
    public  func showXHToastTopWithText(_ text:String,topOffset:CGFloat,duration:CGFloat) {
        
        let toast: XHToast = XHToast(text: text)
        toast.duration = duration
        toast.showIn(self, topOffset: topOffset)
        
    }
    

    //MARK:-下方显示
    
    /// 下方显示
    ///
    /// - Parameter text: 文字
    public func showXHToastBottomWithText(_ text:String){
        self.showXHToastBottomWithText(text, bottomOffset: ToastSpace, duration: ToastDispalyDuration)
    }
    
    
    /// 下方显示+自定义停留时间
    ///
    /// - Parameters:
    ///   - text: 文字
    ///   - duration: 自定义停留时间
    public func showXHToastBottomWithText(_ text:String,  duration:CGFloat){
        
         self.showXHToastBottomWithText(text, bottomOffset: ToastSpace, duration: duration)
        
    }
    
    
    /// 下方显示+自定义到顶部距离
    ///
    /// - Parameters:
    ///   - text: 文字
    ///   - topOffset: 自定义到顶部距离
    public func showXHToastBottomWithText(_ text:String,bottomOffset:CGFloat){
        
        self.showXHToastBottomWithText(text, bottomOffset: bottomOffset, duration: ToastDispalyDuration)
        
    }
    
    /// 下方显示+自定义到顶部距离+自定义停留时间
    ///
    /// - Parameters:
    ///   - text: 文字
    ///   - topOffset: 自定义到顶部距离
    ///   - duration: 自定义停留时间
    public  func showXHToastBottomWithText(_ text:String,bottomOffset:CGFloat,duration:CGFloat) {
        
        let toast: XHToast = XHToast(text: text)
        toast.duration = duration
        toast.showIn(self, bottomOffset: bottomOffset)
    }

}

extension UIWindow
{
    fileprivate class func window() -> UIWindow{
        let window = UIApplication.shared.windows.last!
        if(!window.isHidden){
            return window;
        }
        return (UIApplication.shared.delegate?.window!)!;
    }
}

open class XHToast:NSObject {
    
    var contentView: UIButton
    var duration:CGFloat
    
    init(text: String) {
        
        duration = ToastDispalyDuration
        
        let font = UIFont.boldSystemFont(ofSize: 16)
        let attributes = [NSAttributedString.Key.font: font]
        let rect = text.boundingRect(with: CGSize(width: 250,height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes:attributes, context: nil)
        let textLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: rect.size.width+40, height: rect.size.height+20))
        textLabel.backgroundColor = UIColor.clear
        textLabel.textColor = UIColor.white
        textLabel.textAlignment = NSTextAlignment.center
        textLabel.font = font
        textLabel.text = text
        textLabel.numberOfLines = 0
        contentView = UIButton(frame: CGRect(x: 0, y: 0, width: textLabel.frame.size.width, height: textLabel.frame.size.height))
        contentView.layer.cornerRadius = 20.0
        contentView.backgroundColor = ToastBackgroundColor
        contentView.addSubview(textLabel)
        contentView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth
        
        super.init()
        
        contentView.addTarget(self, action:#selector(toastTaped(_:)), for: UIControl.Event.touchDown)
        contentView.alpha = 0.0
        
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func dismissToast() {
        contentView.removeFromSuperview()
    }
    
    @objc fileprivate func toastTaped(_ sender: UIButton) {
        
        self.hideAnimation()
    }
    
    fileprivate func showAnimation() {
        
        UIView.animate(withDuration: 0.3, delay: 0, options: UIView.AnimationOptions.curveEaseIn, animations: {
            
            self.contentView.alpha = 1.0
            
        }) { (completion) in
            
            
        }
    }
    
    fileprivate  func hideAnimation() {
        
        UIView.animate(withDuration: 0.3, delay: 0, options: UIView.AnimationOptions.curveEaseOut, animations: {
            
            self.contentView.alpha = 0.0
            
        }) { (completion) in
            
            
        }
        
    }
    
    fileprivate func showIn(_ view:UIView) {

        contentView.center = view.center
        view.addSubview(contentView)
        self.showAnimation()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(UInt64(duration) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
            
            self.hideAnimation()
            
        }
    }
    
    fileprivate func showIn(_ view:UIView,topOffset top: CGFloat) {
        
        contentView.center = CGPoint(x: view.center.x, y: top+contentView.frame.size.height/2)
        view.addSubview(contentView)
        self.showAnimation()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(UInt64(duration) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
            
            self.hideAnimation()
        }
    }
    
    fileprivate func showIn(_ view:UIView,bottomOffset bottom: CGFloat) {
        
        contentView.center = CGPoint(x: view.center.x, y: view.frame.size.height-(bottom+contentView.frame.size.height/2))
        view.addSubview(contentView)
        self.showAnimation()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(UInt64(duration) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
            
            self.hideAnimation()
        }
    }
    
}



http://www.niftyadmin.cn/n/4974975.html

相关文章

【GoLang】go入门:go语言执行过程分析 常见数据类型(基本数据类型)

1、go语言执行过程分析 【1】执行流程分析 通过 go build 进行编译 运行上一步生成的可执行文件 通过 go run 命令直接运行 【2】上述两种执行流程的区别 在编译时,编译器会将程序运行时依赖的库文件包含在可执行文件中,所以可执行文件会变大很多通过g…

2005-2022年全国各地级市经济增长目标约束

2005-2022年全国各地级市经济增长目标约束 1、时间:2005-2022年 2、来源:政府工作报告 3、指标:省份、城市、年份、经济增长目标硬约束、经济增长目标软约束 4、范围:地级市,每年具体城市数量参看下面图片 5、指标…

Feign在实际项目中使用详解

Feign在实际项目中使用详解 简介一 Feign客户端应该如何提供?二 Feign调用的接口要不要进行包装?2.1.问题描述2.2.问题解决 三 Feign如何抓取业务生产端的业务异常?3.1.分析3.2.Feign捕获不到异常3.3.异常被额外封装3.4.解决方案 案例源码 简…

Java正则表达式系列--从字符串中提取字符串或数字

原文网址:Java正则表达式系列--从字符串中提取字符串或数字_IT利刃出鞘的博客-CSDN博客 简介 本文用示例介绍Java如何使用正则表达式从字符串中提取想要的内容(字符串或者数字等)。 例1:提取一次不同内容 需求 从字符串中找到…

Element table根据字段合并表格(可多字段合并),附带拖拽列动态合并

效果如图,姓名 数值1 字段进行自动合并 封装合并列js - tableMerge.js // 获取列合并的行数 // params // tableData: 表格数据 // mergeId: 合并的列的字段名 export const tagRowSpan (tableData, mergeId) >{const tagArr [];let pos 0;tableData.map((i…

使用ChatGPT给Python代码写单元测试

先写一个简单的python函数,找chatgpt写单元测试: 有一个python函数,请帮忙写单元测试,函数长这样: def test2(a: list, b: list) -> float:"""计算两个坐标的距离:param a list 格式如&#xff1a…

代码随想录算法训练营第四十八天|LeetCode 583,72,编辑距离总结篇

目录 LeetCode 583.两个字符串的删除操作 动态规划五步曲: 1.确定dp[i][j]的含义 2.找出递推公式 3.初始化dp数组 4.确定遍历方向 5.打印dp数组 LeetCode 72.编辑距离 动态规划五步曲: 1.确定dp[i][j]的含义 2.找出递推公式 3.初始化dp数组 4.确定遍历方…

国际版阿里云/腾讯云:怎么快速建立阿里云服务器

怎么快速建立阿里云服务器 1 简介 ​ 建立服务器的方法能够是购买物理主机,也能够运用在线云服务器(VPS)。 前者需求考虑硬件保护等琐碎问题,所以这儿只介绍后者; 目前云服务器供给商有很多,比如腾讯、阿…