iOS:定制自适应大小的透明吐司弹框

一、简单介绍

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

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

简单好用。

 

二、代码使用

.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/2;
    self.layer.masksToBounds = YES;
}

-(void)setupFrame
{
    CGFloat LabelWidth = [self.messageLabel sizeThatFits:CGSizeMake(MAXFLOAT, HUDHEIGHT)].width;
    LabelWidth = MAX(LabelWidth+2*HUDPADDING, HUDMINWIDTH);
    self.frame = CGRectMake(0, 0, 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(@(0));
        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/2 animations:^{
        self.alpha = 1.0;
    }];
}

-(void)showWithDurationAutoHide:(NSTimeInterval)duration{
    
    [UIView animateWithDuration:DURATION/2 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/2 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/2 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:16];
    }
    return _messageLabel;
}

@end

 

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

posted @ 2019-04-15 10:11  XYQ全哥  阅读(745)  评论(0编辑  收藏  举报