当前位置: 首页 > news >正文

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

CoderZhuXH/XHToastSwift - github

//
//  XHToast.swift
//  XHToastSwiftExample
//
//  Created by xiaohui on 16/8/12.
//  Copyright © 2016年 CoderZhuXH. All rights reserved.
//  代码地址:https://github.com/CoderZhuXH/XHToastSwiftimport 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 = durationtoast.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 = durationtoast.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 = durationtoast.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 = durationtoast.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 = durationtoast.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 = durationtoast.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: UIButtonvar duration:CGFloatinit(text: String) {duration = ToastDispalyDurationlet 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.cleartextLabel.textColor = UIColor.whitetextLabel.textAlignment = NSTextAlignment.centertextLabel.font = fonttextLabel.text = texttextLabel.numberOfLines = 0contentView = UIButton(frame: CGRect(x: 0, y: 0, width: textLabel.frame.size.width, height: textLabel.frame.size.height))contentView.layer.cornerRadius = 20.0contentView.backgroundColor = ToastBackgroundColorcontentView.addSubview(textLabel)contentView.autoresizingMask = UIView.AutoresizingMask.flexibleWidthsuper.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.centerview.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.lryc.cn/news/151570.html

相关文章:

  • Spring Bean 的生命周期,如何被管理的
  • MATLAB算法实战应用案例精讲-【概念篇】量子机器学习
  • 【kubernetes】Argo Rollouts -- k8s下的自动化蓝绿部署
  • vue Cesium接入在线地图
  • OBS Studio 30.0 承诺在 Linux 上支持英特尔 QSV,为 DeckLink 提供 HDR 回放功能
  • springboot整合SpringSecurity
  • 最近在搭建ELK日志平台时,logstash报错JSON parse error
  • 某次护网红队getshell的经历
  • C#实现日期选择器、显示当地时间、跑马灯等功能
  • 如何让看书变听书?
  • pytorch异常——loss异常,不断增大,并且loss出现inf
  • Lua学习(一)
  • Python:列表推导式
  • 应急三维电子沙盘数字孪生系统
  • LeetCode每日一题:1654. 到家的最少跳跃次数(2023.8.30 C++)
  • 数据结构例题代码及其讲解-栈与队列
  • 【Spark】Pyspark RDD
  • 数学建模:Logistic回归预测
  • 一个面向MCU的小型前后台系统
  • 软件外包开发人员分类
  • HTML 元素被定义为块级元素或内联元素
  • 单调递增的数字【贪心算法】
  • gnuradio-hackrf_info.exe -FM频率使用
  • JVM学习(三)--生产环境的线程问题诊断
  • PHP数组处理$arr1转换为$arr2
  • ATF(TF-A)安全通告 TFV-10 (CVE-2022-47630)
  • 详解 SpringMVC 中获取请求参数
  • Message: ‘chromedriver‘ executable may have wrong permissions.
  • 每日一题 1372二叉树中的最长交错路径
  • 【力扣每日一题】2023.9.2 最多可以摧毁的敌人城堡数量