模仿Android toast 写的 Swift 版Toast,支持自动换行。

//
//  Created by 谭尧 on 2021/7/20.
//import Foundation
import UIKitclass Toast {private static var INSTANCE:Toast? = nilpublic static func  makeText(_ text:String)->Toast{if INSTANCE == nil {INSTANCE = Toast()}INSTANCE!.toastText = textreturn INSTANCE!}private let window = UIApplication.shared.windows.first!private let contentView = UIView()private let lableToast = UILabel(frame: CGRect(x: 0, y: 0, width: 240, height: 40))private var toastText = ""private init(){lableToast.text = ""lableToast.textAlignment = .centerlableToast.textColor = .blacklableToast.backgroundColor = .clearlableToast.numberOfLines = 0contentView.frame = lableToast.framecontentView.backgroundColor = .whitecontentView.layer.cornerRadius = 12contentView.layer.borderColor = UIColor.lightGray.cgColorcontentView.layer.borderWidth = 1contentView.center = CGPoint(x: window.center.x, y: window.center.y*7/5)contentView.addSubview(lableToast)}private var timer:DispatchSourceTimer? = nilfunc show(showTimeSec:Int = 3){if timer != nil {hide()}var w = CGFloat(16)*1.2 * CGFloat(toastText.count), h = CGFloat(40)if w > 240 {w = 240h = 70}lableToast.frame.size = CGSize(width: w, height: h)contentView.frame.size = lableToast.frame.sizecontentView.center.x = window.center.xlableToast.text = toastTextcontentView.alpha = 0.0window.addSubview(contentView)easyIn()timer = DispatchSource.makeTimerSource(flags: .strict, queue: DispatchQueue.main)timer?.schedule(deadline: .now() + .seconds(showTimeSec))timer?.setEventHandler(handler: {self.hide()})timer?.resume()}@objc private func hide(){self.contentView.removeFromSuperview()self.timer?.cancel()self.timer = nil}private func easyIn(){UIView.beginAnimations(nil, context: nil)UIView.setAnimationCurve(.easeIn)UIView.setAnimationDuration(0.2)contentView.alpha = 1.0UIView.commitAnimations()}
}

swift Toast相关推荐

  1. Mac之button的使用Show+NSMenu+next+to+NSButton+in+Swift+OSX

    http://www.itdaan.com/keywords/Show+NSMenu+next+to+NSButton+in+Swift+OSX.html http://www.itdaan.com/ ...

  2. swift中WKWebView和JS交互实现

    在Swift中加载HTML网页有两个view,分别是WKWebView和UIWebView,这里我使用的是WKWebView 这里分享一下我的写法: 1.定义变量WKWebView: lazy var ...

  3. Xtend:Android平台的Swift语言

    6月份的苹果开发者大会推出了iOS平台的新编程语言Swift,用以替换陈旧的Objective-C. 尽管Google也善于创建新的语言如Go,Dart,但似乎对于Android平台并未计划有专门的语 ...

  4. iOS开发之swift资料大全

    目录(?)[+] 版本:Swift github排名 https://github.com/trending,github搜索:https://github.com/search 主要工作说明:  1 ...

  5. swift 学习资料大全

    版本:Swift github排名 https://github.com/trending,github搜索:https://github.com/search 主要工作说明:  1. 将Swift从 ...

  6. OC swift 一些常用第三方收集整理 (第三方集合)

    整理了Xcode好用的插件,包括OC和Swift,信息更详细和完整 下拉刷新 EGOTableViewPullRefresh– 最早的下拉刷新控件. SVPullToRefresh– 下拉刷新控件. ...

  7. 全面的Swift学习资料整理

    资料1 ---教程类 官方文档中文翻译http://wiki.jikexueyuan.com/project/swift/Github上的地址点我 Using Swift with Cocoa and ...

  8. Swift与LLVM-Clang原理与示例

    Swift与LLVM-Clang原理与示例 LLVM 学习 从 简单汇编基础 到 Swift 不简单的 a + 1 作为iOS开发,程序崩溃犹如家常便饭,秉着没有崩溃也要制造崩溃的原则 每天都吃的很饱 ...

  9. Can't toast on a thread that has not called Looper.prepare()

    这个错误说了就是子线程直接弹Toast 就说下我的处理方法 Looper.prepare();Toast.makeText(MainActivity.this, "11111", ...

最新文章

  1. BCE支持者在BCH见面会烧毁BCH主题衣服
  2. Oracle 内核参数
  3. 【活动(北京)】Global Azure Bootcamp
  4. 【渝粤教育】国家开放大学2018年春季 0175-21T社会调查研究与方法 参考试题
  5. python2和python3的print语句语法有什么不同_Python3.2的版本,输入print语句总是出错,是什么原因?...
  6. maven打包插件:maven-compiler-plugin、maven-dependency-plugin、maven-jar-plugin、maven-resources-plugin详解
  7. apipost脚本使用二
  8. 工业大数据分析应用在哪方面
  9. 喵哈哈村的魔法考试 Round #14 (Div.2) 题解
  10. 如何在C#中读写INI文件
  11. cgi一键还原 linux分区,cgi plus一键备份恢复工具
  12. 使用Python脚本将酷狗音乐的缓存文件修改为可播放
  13. 在虚拟机中安装windows server 2008
  14. 项目管理师证怎么考?报考条件有哪些?
  15. C++调用JSON-CPP库实现JSON字符串解析
  16. IDEA中对代码进行commit时,git报Unable to create ‘E:/blog/.git/index.lock‘: File exists问题解决
  17. SKYCC组合营销软件的特色
  18. 倒计时1天! | 明日9点,这场精彩的Web3盛宴不容错过
  19. Vmware ubuntu桥接方式联网
  20. lenovo 邵阳E42-80 Ubuntu14.04.5 wireless 驱动安装

热门文章

  1. 一个屌丝程序员的青春(三七九)
  2. 富文本编辑器:ckeditor(使用ckeditor4-vue)
  3. socket编程——upd(数据报套接字)
  4. 中兴网信员工坠楼事件背后:程序员中年危机来了?
  5. 对比农行与建行网银业务办理流程
  6. Ubuntu 安装 XDM 2018 ( Xtreme Download Manager 2018 )
  7. 【Language model】使用RNN LSTM训练语言模型 写出45°角仰望星空的文章
  8. Matplotlib:设置坐标轴范围,刻度,位置,自定义刻度名称,添加数据标签
  9. 计算机控制系统的模拟控制器,导 读 利用计算机代替常规的模拟控制器,使它成为控制系统的一个组成部分,这种有计算机参加控制的系统简称为计算机控制系统。...
  10. Dremel和Hadoop