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.mzph.cn/news/64890.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL高阶语句(三)

一、NULL值 在 SQL 语句使用过程中,经常会碰到 NULL 这几个字符。通常使用 NULL 来表示缺失 的值,也就是在表中该字段是没有值的。如果在创建表时,限制某些字段不为空,则可以使用 NOT NULL 关键字,不使用则默认可以为空…

数据结构学习系列之用队列实现栈功能与用栈实现队列功能

队列与栈:队列(Queue)是一种先进先出(FIFO)的线性表;栈(Stack)是一种后进先出(LIFO)的线性表;实例1:用队列实现栈的功能;算…

架构,平台,框架的区别和联系

1、解释说明 - 架构:在软件开发中,架构是指软件的整体设计和组织方式。它包括了软件的结构、组件和交互方式等方面的设计。架构定义了系统的高级结构和组织方式,以及各个组件之间的关系和交互方式。一个良好的架构可以提高软件的可维护性、可…

中级深入--day16

爬虫(Spider),反爬虫(Anti-Spider),反反爬虫(Anti-Anti-Spider) 之间恢宏壮阔的斗争... Day 1 小黄想要某站上所有的电影,写了标准的爬虫(基于HttpClient库),不断地遍历某站的电影列表页面,根据 Html 分析电影名字存进…

Multisim14.0仿真(五)三角波发生器

一、仿真原理图: 二、仿真效果:

Three.js相机参数及Z-Fighting问题的解决方案

本主题讨论透视相机以及如何为远距离环境设置合适的视锥体。 推荐:用 NSDT编辑器 快速搭建可编程3D场景 透视相机是一种投影模式,旨在模仿人类在现实世界中看待事物的方式。 这是渲染 3D 场景最常用的投影模式。 - three.js 如果你看一下 Three.js 文档…

单元测试:优雅编写Kotlin单元测试

一、MockK简介 MockK是一款功能强大、易于使用的Kotlin mocking框架。在编写单元测试时,MockK能够帮助我们简化代码、提高测试覆盖率,并改善测试的可维护性。除了基本用法外,MockK还提供了许多额外的功能和灵活的用法,让我们能够…

汇编原理学习记录:物理地址=段地址*16+偏移地址

文章目录 知识点个人思考解释存储器大小为1MB段的最大占用存储为64KB物理地址段地址*16偏移地址 知识点 8086计算机拥有20根地址总线和16根数据总线,地址总线中的16根和数据总线存在复用 数据总线的数量决定了数据总线的宽度,决定了处理器的位数&#…

go语言基础操作--二

a : 10str : "mike"//匿名函数,没有函数名字 形成一个闭包,函数定义,还没有调用f1 : func() { //:自动推到类型fmt.Println("a ", a)fmt.Println("str ", str)}f1()//给一个函数类型起别名 这个写法不推荐type FuncType …

04. 函数和函数调用机制

1. 先学习/复习C语言的入门知识 1.1 C语言简介 C语言是一种通用的编程语言,于1972年由丹尼斯里奇(Dennis Ritchie)创建。C语言最初目的是为了开发UNIX操作系统,但由于其简洁的语法、快速的执行速度和可移植性,自此成…

【Python数据分析】数据分析之numpy基础

实验环境:建立在Python3的基础之上 numpy提供了一种数据类型,提供了数据分析的运算基础,安装方式 pip install numpy导入numpy到python项目 import numpy as np本文以案例的方式展示numpy的基本语法,没有介绍语法的细枝末节&am…

03-Flask-工程配置加载方式

工程配置加载方式 前言配置对象中加载配置文件中加载环境变量中加载三种配置方式优缺点工厂模式创建Flask app 前言 本篇来学习下Flake工程配置加载方式 配置对象中加载 应用场景:作为默认配置写在代码中 # -*- coding: utf-8 -*- # Time : 2023/9/2 # Autho…

String实例化的区别

面试题:请解释String类中两种对象实例化的区别 String str “hello”(常量池对象) 只会开辟一块堆内存空间,保存在字符串常量池中,然后str共享常量池中的String对象String str new String(“hello”)(常量…

ZooKeeper基础命令和Java客户端操作

1、zkCli的常用命令操作 (1)Help (2)ls 使用 ls 命令来查看当前znode中所包含的内容 (3)ls2查看当前节点数据并能看到更新次数等数据 (4)stat查看节点状态 (5&#xf…

【ROS系统】Ubuntu22.04系统中安装ROS2系统_ubuntu 安装ros2_GoesM

【ROS系统】Ubuntu22.04系统中安装ROS2系统_ubuntu 安装ros2_GoesM Excerpt ROS仿真、专为自动驾驶研发提供的系统平台_ubuntu 安装ros2 参考博客:ROS 安装详细教程 —— Ubuntu22.0.4 LTS 安装 Part 0. 准备 首先,我们需要一个Ubuntu系统。 Part 1. …

Spring Boot复用yaml文件

如果Spring Boot项目的配置过多,可以把一些配置项放在单独的yaml文件中,并以application-xxx.yaml命名。这些配置文件可以保存在当前项目的resources目录中,也可以保存在maven依赖的resources目录中,并在当前项目引入。 在applic…

【kubernetes】Argo Rollouts -- k8s下的自动化蓝绿部署

蓝绿(Blue-Green)部署简介 在现代软件开发和交付中,确保应用程序的平稳更新和发布对于用户体验和业务连续性至关重要。蓝绿部署是一种备受推崇的部署策略,它允许开发团队在不影响用户的情况下,将新版本的应用程序引入生产环境。 蓝绿部署的核心思想在于维护两个独立的环…

ESP32C3 LuatOS RC522①写入数据并读取M1卡

LuatOS RC522官方示例 官方示例没有针对具体开发板,现以ESP32C3开发板为例。 选用的RC522模块 ESP32C3-CORE开发板 注意ESP32C3的 SPI引脚位置,SPI的id2 示例代码 -- LuaTools需要PROJECT和VERSION这两个信息 PROJECT "helloworld" VERSIO…

前端list列表自定义图标并设置大小

前端list列表自定义图标并设置大小 一、前端list列表基础知识回顾 前端公有两种列表,一种是有序列表(ol),一种是无序列表(ul),它们的子元素都是(li)。 1.1 有序列表&a…

4.0 Spring与Dubbo整合原理与源码分析

#Dubbo# 文章介绍 Dubbo中propertie文件解析以及处理原理Dubbo中@Service注解解析以及处理原理Dubbo中@Reference注解解析以及处理原理1.0 整体架构和流程 应用启动类与配置 public class Application {public static void main(String[] args) throws Exception {Annotation…