三、模仿QQ:顶部显示提示信息,1-2行,2秒自动消失

PS:工程中需单独添加 Masonry 第三方库

创建一个类,我这里类名ToastVC,继承自UIViewController

在.h文件中

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, ToastStatus) {
    ToastStatus_Success,
    ToastStatus_Error,
    ToastStatus_Info,
};

@interface ToastVC : UIViewController

+ (void)toastWithTitle:(NSString *)title status:(ToastStatus)status owner:(UIViewController *)owner;

@end

在.m文件中

#import "ToastVC.h"

CGFloat statusBarHeight = 0.0;

@interface ToastVC ()

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSArray *colors;

@end

@implementation ToastVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    statusBarHeight = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;
}

// 模仿QQ:顶部显示提示信息,1-2行,2秒自动消失
+ (void)toastWithTitle:(NSString *)title status:(ToastStatus)status owner:(UIViewController *)owner {
    
    ToastVC *v = [[ToastVC alloc] init];
    [[UIApplication sharedApplication].windows.firstObject addSubview:v.view];
    [v.button setTitle:title forState:UIControlStateNormal];
    v.view.backgroundColor = v.colors[status];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = statusBarHeight + owner.navigationController.navigationBar.frame.size.height;
    v.view.frame = CGRectMake(0, -height, width, height);
    [UIView animateWithDuration:0.3 animations:^{
        v.view.frame = CGRectMake(0, 0, width, height);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 animations:^{
            v.view.frame = CGRectMake(0, -height, width, height);
        } completion:^(BOOL finished) {
            [v.view removeFromSuperview];
        }];
    });
}

#pragma mark - SET/GET

- (UIButton *)button {
    
    if (_button == nil) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.clipsToBounds = YES;
        _button.layer.masksToBounds = YES;
        [_button.titleLabel setNumberOfLines:2];
        [_button.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:_button];
        [_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(statusBarHeight);
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(8);
            make.right.mas_equalTo(-8);
        }];
    }
    
    return _button;
}

- (NSArray *)colors {
    
    if (_colors == nil) {
        _colors = @[[UIColor greenColor], [UIColor redColor], [UIColor orangeColor]];
    }
    
    return _colors;
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值