Objective-C仿映客顶部Toast提示

本文介绍了一种Objective-C实现的顶部Toast提示,模仿了映客直播的样式,支持队列显示。提供了相关宏定义、属性,并给出了代码实现和调用示例。项目已上传至GitHub。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近直播很火,尤以映客为甚,所以仿照映客,造了一个顶部toast提示的小轮子,支持队列显示。
开放除了这些接口,如果不够需要的话可以自己再加一些设置:

+ (void)showToptoastWithText:(NSString *)text;
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration;
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration height:(float)height;
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration height:(float)height backGroundColor:(UIColor *)color;
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration height:(float)height backGroundColor:(UIColor *)color alpha:(float)alpha;

一些宏定义:

//默认停留时间
#define ToastDispalyDuration 2.0f
//默认背景色
#define ToastBackgroundColor [UIColor purpleColor]
//默认高度
#define ToastHeight 64

一些属性:

@property (nonatomic, strong) UIButton *contentView;
@property (nonatomic, assign) CGFloat duration;
@property (nonatomic, assign) CGFloat height;

@implementation中代码:

//多次点击缓存,逐个显示
static NSMutableArray *_toastArr;

- (void)dealloc{
    NSLog(@"VVLiveToast dealloc");
}

- (id)initWithText:(NSString *)text{
    if (self = [super init]) {
        _duration = ToastDispalyDuration;
        _height = ToastHeight;
        if (_toastArr.count == 0) {
            _toastArr = [[NSMutableArray alloc]init];
        }

        _contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, -_height, [UIScreen mainScreen].bounds.size.width, _height)];
        _contentView.backgroundColor = ToastBackgroundColor;
        [_contentView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)]];

        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, _height)];
        textLabel.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, _height);
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textColor = [UIColor whiteColor];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = [UIFont boldSystemFontOfSize:15];
        textLabel.text = text;
        textLabel.numberOfLines = 0;
        [_contentView addSubview:textLabel];

    }
    return self;
}
//设置停留时间
- (void)setDuration:(CGFloat)duration{
    _duration = duration;
}
//设置背景色
- (void)setBackgroundColor:(UIColor *)color{
    _contentView.backgroundColor = color;
}
//设置高度
- (void)setHeigh:(float)height{
    _height = height;
}
//设置透明度
- (void)setBackgroundAlpha:(float)alpha{
    _contentView.alpha = alpha;
}
//出现动画
-(void)showAnimationWithToast:(TopToast *)toast{
    [UIView beginAnimations:@"show" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.3];
    toast.contentView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, toast.height);
    [UIView commitAnimations];
}
//影藏动画
-(void)hideAnimationWithToast:(TopToast *)toast{
    [UIView animateWithDuration:0.3 animations:^{
        toast.contentView.frame = CGRectMake(0, -toast.height, [UIScreen mainScreen].bounds.size.width, toast.height);
    } completion:^(BOOL finished) {
        [toast.contentView removeFromSuperview];
        //影藏后检查是否有toast缓存
        if (_toastArr.count > 0) {
            [_toastArr removeObjectAtIndex:0];
            if (_toastArr.count > 0) {
                [self showWithToast:_toastArr[0]];
            }
        }
    }];
}
//触摸隐藏
- (void)hide{
    [UIView animateWithDuration:0.3 animations:^{
        _contentView.frame = CGRectMake(0, -_height, [UIScreen mainScreen].bounds.size.width, _height);
    }completion:^(BOOL finished) {
        [_contentView removeFromSuperview];
    }];
}

- (void)showWithToast:(TopToast *)toast{
    [[UIApplication sharedApplication].keyWindow addSubview:toast.contentView];
    [self showAnimationWithToast:toast];

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_duration * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self hideAnimationWithToast:toast];
    });
}

+ (void)showToptoastWithText:(NSString *)text{
    TopToast *toast = [[TopToast alloc] initWithText:text];
    [toast setDuration:ToastDispalyDuration];
    if (_toastArr.count == 0) {
        [toast showWithToast:toast];
    }
    [_toastArr addObject:toast];
}
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration{
    TopToast *toast = [[TopToast alloc] initWithText:text];
    if (_toastArr.count == 0) {
        [toast showWithToast:toast];
    }
    [_toastArr addObject:toast];
}
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration height:(float)height{
    TopToast *toast = [[TopToast alloc] initWithText:text];
    [toast setDuration:duration];
    [toast setHeigh:height];
    if (_toastArr.count == 0) {
        [toast showWithToast:toast];
    }
    [_toastArr addObject:toast];
}
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration height:(float)height backGroundColor:(UIColor *)color{
    TopToast *toast = [[TopToast alloc] initWithText:text];
    [toast setDuration:duration];
    [toast setHeigh:height];
    [toast setBackgroundColor:color];
    if (_toastArr.count == 0) {
        [toast showWithToast:toast];
    }
    [_toastArr addObject:toast];
}
+ (void)showToptoastWithText:(NSString *)text duration:(CGFloat)duration height:(float)height backGroundColor:(UIColor *)color alpha:(float)alpha{
    TopToast *toast = [[TopToast alloc] initWithText:text];
    [toast setDuration:duration];
    [toast setHeigh:height];
    [toast setBackgroundColor:color];
    [toast setBackgroundAlpha:alpha];

    if (_toastArr.count == 0) {
        [toast showWithToast:toast];
    }
    [_toastArr addObject:toast];

}

调用:

    [TopToast showToptoastWithText:@"TopToast" duration:2 height:64 backGroundColor:[UIColor blueColor] alpha:1.0];

GitHub地址:https://github.com/FEverStar/TopToastDemo
效果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值