在实际开发中,想要实现类似于Android系统自带的Toast弹出信息提示窗,那么该如何实现呢?相信大家对于windows窗口并不陌生,此处的实现思路是利用UIApplication中windows窗口添加视图,设置定时消失实现类似于Android系统自带Toast弹出信息提示窗。

ToastUtil.h

#import <UIKit/UIKit.h>typedef NS_ENUM(NSInteger, Gravity) {TOP,CENTRE,BOTTOM
};@interface ToastUtil : NSObject+(instancetype)toast;// 显示Toast(默认时长2.5秒,默认底部显示)
-(void)showToast:(NSString *)msg;// 显示Toast:设置时长(默认底部显示)
-(void)showToast:(NSString *)msg duration:(NSTimeInterval)duration;// 显示Toast:设置时长,设置显示位置(TOP,CENTRE,BOTTOM)
-(void)showToast:(NSString *)msg duration:(NSTimeInterval)duration gravity:(Gravity)gravity;// 显示Toast:设置时长,设置显示位置(TOP,CENTRE,BOTTOM),设置消失回调监听
-(void)showToast:(NSString *)msg duration:(NSTimeInterval)duration gravity:(Gravity)gravity finishHandler:(dispatch_block_t)finishHandler;@end

ToastUtil.m

#import <Foundation/Foundation.h>
#import "ToastUtil.h"#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0
#define MultilineTextSize(text, font, maxSize) [text length] > 0 ? [text \
boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin) \
attributes:@{NSFontAttributeName:font} context:nil].size : CGSizeZero;
#else
#define MultilineTextSize(text, font, maxSize) [text length] > 0 ? [text \
sizeWithFont:font constrainedToSize:maxSize] : CGSizeZero;
#endif// 获取屏幕的宽度和高度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)// 文字视图间距
const CGFloat toast_padding = 12;
// 视图圆角半径
const CGFloat toast_cornerRadius = 4;
// 字体大小
const CGFloat toast_fontSize = 12;// Toast显示透明度动画时间
const NSTimeInterval ANIM_DURATION = 0.2;
// 默认Toast显示时间
const NSTimeInterval Toast_DEFAULT_DURATION = 2.5;@interface ToastUtil()@property (strong, nonatomic) UIView *toastView;
@property (nonatomic, strong) NSTimer *toastTimer;
@property (nonatomic, strong) NSTimer *dismissTimer;
@property (nonatomic, copy) dispatch_block_t finishHandler;@end@implementation ToastUtil@synthesize toastView;
@synthesize toastTimer;
@synthesize dismissTimer;
@synthesize finishHandler;+ (instancetype)toast{static ToastUtil *instance;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{instance = [[ToastUtil alloc] init];});return instance;
}- (void)showToast:(NSString *)msg{[self toastShow:msg duration:Toast_DEFAULT_DURATION gravity:BOTTOM finishHandler:^{NSLog(@"Toast消失:msg:%@",msg);}];
}- (void)showToast:(NSString *)msg duration:(NSTimeInterval)duration{[self toastShow:msg duration:duration gravity:BOTTOM finishHandler:^{NSLog(@"Toast消失:msg:%@",msg);}];
}- (void)showToast:(NSString *)msg duration:(NSTimeInterval)duration gravity:(Gravity)gravity{[self toastShow:msg duration:duration gravity:gravity finishHandler:^{NSLog(@"Toast消失:msg:%@",msg);}];
}- (void)showToast:(NSString *)msg duration:(NSTimeInterval)duration gravity:(Gravity)gravity finishHandler:(dispatch_block_t)finishHandler{[self toastShow:msg duration:duration gravity:gravity finishHandler:finishHandler];
}-(void)toastShow:(NSString *)msg duration:(NSTimeInterval)duration gravity:(Gravity)gravity finishHandler:(dispatch_block_t)finishHandler{self.finishHandler = finishHandler;if (toastView || toastView.superview) {[self removeToast];}[self initToastView:msg gravity:gravity];if (toastView) {toastView.alpha = 0;[[[UIApplication sharedApplication].windows firstObject] addSubview:toastView];[[[UIApplication sharedApplication].windows firstObject] bringSubviewToFront:toastView];}[UIView animateWithDuration:ANIM_DURATION animations:^{if (self->toastView) {self->toastView.alpha = 1;}}];toastTimer = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(finishDismiss) userInfo:nil repeats:NO];
}- (void)initToastView:(NSString *)msg gravity:(Gravity)gravity{CGSize toastSize = [self getToastSizeWithMessage:msg];CGFloat toastWidth = toastSize.width;CGFloat toastHeight = toastSize.height;CGFloat x = (SCREEN_WIDTH - toastWidth) / 2;CGFloat y = (SCREEN_HEIGHT - toastHeight) * 5 / 6;switch (gravity) {case TOP:y = (SCREEN_HEIGHT - toastHeight) * 1 / 6;break;case CENTRE:y = (SCREEN_HEIGHT - toastHeight) / 2;break;case BOTTOM:y = (SCREEN_HEIGHT - toastHeight) * 5 / 6;break;}toastView = [[UIView alloc] init];toastView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];toastView.layer.cornerRadius = toast_cornerRadius;toastView.frame = CGRectMake(x, y, toastWidth, toastHeight);UILabel *messageLabel = [[UILabel alloc] initWithFrame:toastView.bounds];messageLabel.text = msg;messageLabel.textColor = [UIColor whiteColor];messageLabel.font = [UIFont systemFontOfSize:toast_fontSize];messageLabel.numberOfLines = 0;messageLabel.textAlignment = NSTextAlignmentCenter;// 文字自适应宽高CGSize expectSize = [messageLabel sizeThatFits:CGSizeMake(toastWidth, SCREEN_HEIGHT)];messageLabel.frame = CGRectMake((toastWidth - expectSize.width) / 2, (toastHeight - expectSize.height) / 2, expectSize.width, expectSize.height);[toastView addSubview:messageLabel];[toastView addSubview:messageLabel];
}- (void)finishDismiss {[UIView animateWithDuration:ANIM_DURATION animations:^{if (self->toastView) {self->toastView.alpha = 0;}}];dismissTimer = [NSTimer scheduledTimerWithTimeInterval:ANIM_DURATION target:self selector:@selector(dismiss) userInfo:nil repeats:NO];
}- (void) dismiss{[self removeToast];if (self->finishHandler) { self->finishHandler(); }
}- (void)removeToast {if (toastView) {[toastView removeFromSuperview];toastView = nil;}
}- (void)dealloc {if (toastTimer) {[toastTimer invalidate];toastTimer = nil;}if (dismissTimer) {[dismissTimer invalidate];dismissTimer = nil;}
}- (CGSize)getToastSizeWithMessage:(NSString *)msg{UIFont *font = [UIFont systemFontOfSize:toast_fontSize];CGSize textSize = MultilineTextSize(msg, font, CGSizeMake(SCREEN_WIDTH - 60, SCREEN_HEIGHT ));CGFloat labelWidth = textSize.width + 1;CGFloat labelHeight = textSize.height + 1;CGFloat heightPadding = 2 * toast_padding;CGFloat toastHeight = labelHeight + heightPadding;CGFloat toastWidth = labelWidth + heightPadding;return CGSizeMake(toastWidth, toastHeight);
}@end

IOS 实现Toast提示信息弹窗相关推荐

  1. python如何监听toast提示信息_python 怎样获取toast?

    toast是什么? 想要获取toast的小伙伴们,肯定知道这个是一个什么玩意,例行还是加一个图,加以解释,下图的就是传说中的toast,它有一个特点,出现时间特别短,很难通过定位元素去获取这个toas ...

  2. Toast(提示信息),Dialog(弹窗).

    Toast Toast就是显示一个提示信息,它没有焦点,不接受点击事件.主要掌握Toast.makeText(), toast.setGravity()(位置定位),toast.setDuration ...

  3. ionic 的Toast提示信息

    ionic 有多种方法提示信息,在 这使用ngcordova的$cordovaToast  . 1.在项目中执行 cordova plugin add https://github.com/EddyV ...

  4. IOS调试移动端弹窗遮罩input框focus和click事件失效

    移动端的登录窗口绝大多数的做法是点击登录,然后弹出固定定位的遮罩窗口,输入用户名.密码等信息进行登录. 在IOS系统下,当input唤起键盘时,会导致遮罩下层的页面出现滚动和底部空白,这时就会影响到遮 ...

  5. -webkit-overflow-scrolling:touch导致ios中z-index失效(弹窗层级设置无效)

    解决方法: 直接去掉-webkit-overflow-scrolling:touch[不合理,页面卡顿] 弹窗放到根位置,也就是跟遮布平级[根下不存在含有-webkit-overflow-scroll ...

  6. layui提示信息弹窗

    1,layer.msg('只想弱弱提示');, 2,layer.msg('有表情地提示', {icon: 6}); 3,layer.msg('关闭后想做些什么', function(){ //do s ...

  7. toast 弹窗 js

    使用js封装一个全局Toast提示弹窗组件,不使用UI库 export const Toast = {data() {return {}},mounted() {},methods: {// Toas ...

  8. 模态弹窗与非模态弹窗

    在手机app应用中各种格式的弹窗效果相信大家都看过,也可能反感过某些弹窗,本文就来谈谈关于app弹窗设计以及弹窗的适用情景. 一.弹窗的定义 1.弹窗作用 弹窗是为了让用户回应,需要用户与之交互的窗口 ...

  9. 抖音iOS最复杂功能的重构之路--播放器交互区重构实践

    背景介绍 本文以抖音中最为复杂的功能,也是最重要的功能之一的交互区为例,和大家分享一下此次重构过程中的思考和方法,主要侧重在架构.结构方面. 交互区简介 交互区是指播放页面中可以操作的区域,简单理解就 ...

最新文章

  1. 构建node.js基础镜像_我如何使用Node.js构建工作抓取网络应用
  2. 斯坦福前校长John Hennessy、张亚勤等一众大佬云集,共探最前沿技术 | CNCC2020
  3. BRCM5.02编译四: ERROR: lzo/lzo1x.h development library is required for build
  4. 点播同时并发怎么算带宽_如何搭建一个视频点播系统?
  5. 暑期学校 | 东南大学2021年国际暑期学校项目:从感知理解到智能认知 (知识图谱及应用课程)...
  6. java 写 gz_java简写名词解释 - osc_gzyujipq的个人空间 - OSCHINA - 中文开源技术交流社区...
  7. 14 递归函数、二分法
  8. linux malloc free 内存碎片_内存申请malloc/new与内存释放free/delete的区别
  9. php缩放gif和png图透明背景变成黑色的解决方法_php技巧
  10. 关于latex的网站推荐
  11. 原生js之同级元素添加移除class
  12. Atititcmd cli环境变量的调用设置与使用
  13. svn 服务器修改密码,用户自行修改svn密码的简单服务
  14. java生成mib文件_SNMP之MIB文件创建
  15. 计算机文件夹隐藏了怎么恢复,文件夹隐藏了怎么恢复,文件夹设为隐藏如何恢复...
  16. MySQL frm、MYD、MYI数据文件恢复
  17. 蚂蚁区块链BaaS平台应用开发指南(一):前言
  18. php中如何过滤关键字,PHP - 过滤关键字
  19. 低碳世界杂志低碳世界杂志社低碳世界编辑部2022年第7期目录
  20. C语言对文本进行断句,用TextView实现富文本展示,点击断句和语音播报

热门文章

  1. ESXi VM的磁盘模式
  2. 利用streamline函数绘制电力线
  3. BCGControlBar Pro for MFC v13.3现已发布:着重改进图表、属性/编辑控件等,打造全面、易上手的MFC库
  4. 投资理财-1000万资金配置
  5. 中国部分***资料(借鉴高手所学)
  6. 读书笔记系列1——MySQL必知必会
  7. 地球 three.js 城市 3d
  8. 妙味课堂 - 前端初窥 -
  9. 思路分享 | 看我如何给微信下钩子[转]
  10. 机房智能网络监控系统一体解决方案