一、简单介绍

创建一个吐司消息的黑色透明弹框,可以根据消息长短自适应大小。

可以手动创建手动显示手动关闭,也可以手动创建自动显示自动关闭。

简单好用。

二、代码使用

.h文件

//
// LiveHUD.h
//
// Created by 夏远全 on 2019/4/10.
// Copyright © 2019年 xiaoshuang. All rights reserved.
// #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN static CGFloat const HUDHEIGHT = 44.0;
static CGFloat const HUDMINWIDTH = 230.0;
static CGFloat const HUDPADDING = 47.0;
static NSTimeInterval const DURATION = 1.0; @interface LiveHUD : UIView
@property (nonatomic, assign) CGFloat bgAlpha; //背景透明度
@property (nonatomic, strong) UILabel *messageLabel;//消息label //直接显示(默认1.0秒后会自动隐藏)
+(void)showLiveHUDWithMessage:(NSString *)message inView:(UIView *)containerView;
+(void)showLiveHUDWithMessage:(NSString *)message messageFont:(UIFont *)messageFont messageColor:(UIColor *)messageColor inView:(UIView *)containerView; //只是创建
+(instancetype)createLiveHUDWithMessage:(NSString *)message inView:(UIView *)containerView;
+(instancetype)createLiveHUDWithMessage:(NSString *)message messageFont:(UIFont *)messageFont messageColor:(UIColor *)messageColor inView:(UIView *)containerView; //手动显示
-(void)showWithNeedHandHide;
-(void)showWithDurationAutoHide:(NSTimeInterval)duration; //手动隐藏
-(void)hide;
-(void)hideWithDuration:(NSTimeInterval)duration; //更新文案
-(void)updateMessage:(NSString *)message; @end NS_ASSUME_NONNULL_END

.m文件

//
// LiveHUD.m
//
// Created by 夏远全 on 2019/4/10.
// Copyright © 2019年 xiaoshuang. All rights reserved.
// #import "LiveHUD.h" @interface LiveHUD ()
@property (nonatomic, strong) UIView *containerView;
@end @implementation LiveHUD #pragma mark - life cycle
+(instancetype)createLiveHUDWithMessage:(NSString *)message inView:(UIView *)containerView
{
LiveHUD *hud = [[self alloc] init];
hud.messageLabel.text = message;
hud.containerView = containerView;
[hud setup];
return hud;
} +(instancetype)createLiveHUDWithMessage:(NSString *)message messageFont:(UIFont *)messageFont messageColor:(UIColor *)messageColor inView:(UIView *)containerView
{
LiveHUD *hud = [[self alloc] init];
hud.messageLabel.text = message;
hud.messageLabel.font = messageFont;
hud.messageLabel.textColor = messageColor;
hud.containerView = containerView;
[hud setup];
return hud;
} +(void)showLiveHUDWithMessage:(NSString *)message inView:(UIView *)containerView
{
LiveHUD *hud = [[self alloc] init];
hud.messageLabel.text = message;
hud.containerView = containerView;
[hud setup];
[hud show];
} +(void)showLiveHUDWithMessage:(NSString *)message messageFont:(UIFont *)messageFont messageColor:(UIColor *)messageColor inView:(UIView *)containerView
{
LiveHUD *hud = [[self alloc] init];
hud.messageLabel.text = message;
hud.messageLabel.font = messageFont;
hud.messageLabel.textColor = messageColor;
hud.containerView = containerView;
[hud setup];
[hud show];
} -(void)setup
{
[self setDefalut];
[self setupFrame];
[self addSubViews];
[self setupSubviewsConstraints];
} -(void)setDefalut
{
self.alpha = 0.0;
self.backgroundColor = [HEXCOLOR(0X000000) colorWithAlphaComponent:0.7];
self.layer.cornerRadius = HUDHEIGHT/;
self.layer.masksToBounds = YES;
} -(void)setupFrame
{
CGFloat LabelWidth = [self.messageLabel sizeThatFits:CGSizeMake(MAXFLOAT, HUDHEIGHT)].width;
LabelWidth = MAX(LabelWidth+*HUDPADDING, HUDMINWIDTH);
self.frame = CGRectMake(, , LabelWidth, HUDHEIGHT);
self.center = self.containerView.center;
} #pragma mark - add subViews
-(void)addSubViews
{
[self addSubview:self.messageLabel];
[self.containerView addSubview:self];
} #pragma mark - layout subviews
-(void)setupSubviewsConstraints
{
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(@());
make.height.equalTo(@(HUDHEIGHT));
}];
} #pragma mark - event response #pragma mark - public methods
-(void)setBgAlpha:(CGFloat)bgAlpha{
_bgAlpha = bgAlpha;
self.backgroundColor = [HEXCOLOR(0X000000) colorWithAlphaComponent:bgAlpha];
} -(void)showWithNeedHandHide{ [UIView animateWithDuration:DURATION/ animations:^{
self.alpha = 1.0;
}];
} -(void)showWithDurationAutoHide:(NSTimeInterval)duration{ [UIView animateWithDuration:DURATION/ animations:^{
self.alpha = 1.0;
} completion:^(BOOL finished) {
[self hideWithDuration:duration];
}];
} -(void)hide{
[self hideWithDuration:0.0];
} -(void)hideWithDuration:(NSTimeInterval)duration{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:DURATION/ animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}]; });
} -(void)updateMessage:(NSString *)message
{
self.messageLabel.text = message;
[self setupFrame];
} #pragma mark - private methods
-(void)show{ [UIView animateWithDuration:DURATION/ animations:^{
self.alpha = 1.0;
} completion:^(BOOL finished) {
[self hideWithDuration:DURATION];
}];
} #pragma mark - setters #pragma mark - getters
-(UILabel *)messageLabel{
if (!_messageLabel) {
_messageLabel = [[UILabel alloc] init];
_messageLabel.textAlignment = NSTextAlignmentCenter;
_messageLabel.textColor = HEXCOLOR(0XFFFFFF);
_messageLabel.font = [UIFont systemFontOfSize:];
}
return _messageLabel;
} @end

三、参看效果 (触发事件)

最新文章

  1. javascript原型Prototype【转】
  2. JavaScript---Ajax和函数回调,异步编程
  3. mac 无法连接android手机进行调试 解决方案
  4. 特效TD 的工作准则
  5. sqoop安装与使用
  6. Java-Spring MVC如何返回一个非JSP文件名字的地址
  7. 【原】font-awesome IE6支持代码本人测试成功
  8. MYSQL 二进制安装
  9. Java运算符和引用数据类型(Scanner、Random)
  10. Failed while changing version of Java to 1.8.
  11. Linux 内核里的“智能指针”【转】
  12. HDU-6395-矩阵快速幂
  13. Problem B: 平面上的点——Point类 (II)
  14. golang简单实现jwt验证(beego、xorm、jwt)
  15. thinkphp 5.0手记
  16. Maven系列(二)exec-maven-plugin
  17. JS 操作iframe
  18. JavaWeb—基于Token的身份验证
  19. js原码工具集
  20. detach()之大坑:detach会引起局部变量失效引起线程对内存的非法访问题。

热门文章

  1. Docker 二进制安装docker
  2. js自定义滚动样式
  3. springboot(@Service,@Mapper)注解失效导致无法注入service和mapper
  4. Kafka-API
  5. 给定数组长度2n,分成n对,求n对最小元素之和最大
  6. 为什么分布式一定要有Redis?
  7. 利用log4添加log
  8. python3 在 windows 读取路径多了一个\u202a 是咋回
  9. mybatis查询语句的背后之封装数据
  10. 一种表达式语言的解析引擎JEXL简单使用