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

这篇具有很好参考价值的文章主要介绍了iOS swift5 弹出提示文字(停留1~2s)XHToastSwift。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

CoderZhuXH/XHToastSwift - github文章来源地址https://www.toymoban.com/news/detail-685205.html

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


到了这里,关于iOS swift5 弹出提示文字(停留1~2s)XHToastSwift的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ios swift5 “Sign in with Apple“(使用苹果登录)怎样接入(第三方登录)集成AppleID登录

    1.1 如果你新建app id,记得在新建的时候就选中Sign in with Apple功能 1.2 如果app已经上线了,后面再需要加苹果登录的功能,也可以在app id的配置中加这个功能,只是勾选Sign in with Apple点击Save后,profilex需要重新生成 iOS 苹果登录(第三方登录) - csdn 集成AppleID登录 - 简书 要在你的

    2024年04月09日
    浏览(39)
  • iOS开发Swift-6-深色模式,类与对象,MVC模式,弹出框,闭包-趣味问答App

    1.创建趣味问答App项目  2.创建一个问题文本,水平居中约束。 创建蓝、红两个按钮,放入Stack View中,给StackView水平居中约束,下边约束,设置两按钮间距为20.  设置进度条view与safe View关系为equal width。设置他们的比例为1:13.  3.为系统增加深色模式适配(仅限iOS13以上版本

    2024年02月10日
    浏览(29)
  • iOS开发Swift-7-得分,问题序号,约束对象,提示框,类方法与静态方法-趣味问答App

    1.根据用户回答计算得分  ViewController.swift: 2.显示题目序号  ViewController.swift: 3.为屏幕进度条更改约束 将1:13的宽度约束拖入ViewController。  因为progressBarView是只读,所以要根据屏幕宽度计算出1/13的宽度,然后加到Constant中。  ViewController.swift:  4.制作弹窗 https://github.com/rel

    2024年02月10日
    浏览(34)
  • ios 配置了代理且使用 chls.pro/ssl 下载不了证书,无法弹出下载证书的提示

    在使用ios 连接charles时遇到一个问题~ 配置代理且使用 chls.pro/ssl 下载不了证书,浏览器不弹下载证书的提示 1、下载证书 使用默认浏览器打开这个链接:https://www.charlesproxy.com/assets/legacy-ssl/charles.crt 直接下载证书。注:使用数据流量打开该链接 点允许后,会下载证书 2、安装

    2024年02月12日
    浏览(34)
  • Swift中键盘的弹出隐藏,页面抬高,Return键等的配置

    目录 1.点击键盘外的区域时键盘隐藏 2.点击输入框时抬高整体页面,防止输入框被键盘遮挡 两个function的添加

    2024年02月09日
    浏览(33)
  • iOS开发Swift-枚举

    枚举:一组相关的值定义了一个共同的类型,使你可以在代码中以类型安全的方式来使用这些值。 原始值:定义枚举时被预先填充的值。 (1)整数为原始值时,隐式赋值递增1。未设置原始值时,默认为0,之后递增1. (2)字符串为原始值,隐式赋值为枚举成员的名称。

    2024年02月11日
    浏览(36)
  • iOS开发Swift-闭包

    将很长的闭包表达式作为最后一个参数传递给函数,不用写出他的参数标签。 嵌套函数可捕获其外部函数所有参数、变量、常量。 当一个闭包作为一个参数传到一个函数中,但闭包在函数返回之后才被执行,则称闭包逃逸。 标注@escaping,表示允许闭包逃逸。  包装传递给函数

    2024年02月11日
    浏览(39)
  • IOS-生命周期-Swift

    App主要有五种状态,分别是: 未运行——Not running 应用程序没启动 未激活——Inactive 程序在前台运行,不过没有接收到事件。 一般每当应用要从一个状态切换到另一个不同的状态时,中途过渡会短暂停留在此状态。唯一在此状态停留时间比较长的情况是:当用户锁屏时,或

    2024年01月23日
    浏览(44)
  • iOS开发Swift-函数

     (1)无参函数 (2)多参函数 (3)无返回值 (4)多重返回值 (5)可选元组返回类型(元组可以是nil) (6)隐式返回的函数 任一可以被写成一行return的函数,return(x) + for。 调用的时候: 方法名(for: 参数) (1)指定参数标签 (2)忽略参数标签 (3)默认参数值 (4)可变参数 一个可变参数可接受0个或多

    2024年02月11日
    浏览(39)
  • IOS-闭包学习-Swift

    闭包是自包含的函数代码块,可以在代码中被传递和使用。Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks)以及其他一些编程语言中的匿名函数(Lambdas)比较相似。 闭包可以捕获和存储其所在上下文中任意常量和变量的引用。被称为包裹常量和变量。 Swift 会为你管理在捕

    2024年01月24日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包