iOS_开发实战

三种项目中很常见的提示小弹窗

2020-04-29  本文已影响0人  Andy_Livings

我们在开发项目的时候,可能会遇到各种样式的弹窗,并且每个项目的弹窗样式都会千差万别。但是不管他们的样式再怎么变化,只要我们掌握了核心的自定义弹窗方法,那么我们都会很快就能实现这些样式的弹窗的。

今天的主要内容是实现在项目中三种比较常见的提示信息的小弹窗。
效果图如下:

Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-29 at 12.28.30.png Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-29 at 12.28.33.png Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-29 at 12.28.36.png

下面就分别对每个部分的内容进行实现。

1、纯文本提示信息
/// 纯文本提示信息
/// @param message 提示信息
+ (instancetype)showCommonTipPopoViewWithMessage:(NSString *)message;
2、有显示图标的提示信息
/// 有显示图标的提示信息
/// @param message 提示信息
+ (instancetype)showCommonTipPopoViewWithTipMessage:(NSString *)message;
3、有显示错误提示图标的提示信息
/// 有显示错误提示图标的提示信息
/// @param message 提示信息
+ (instancetype)showCommonTipPopoViewWithErrorMessage:(NSString *)message;

下面来说说弹窗的加载方式

+ (instancetype)showCommonTipPopoViewWithMessage:(NSString *)message {
    
    if (_commonTipPopoViewManager == nil) {
        _commonTipPopoViewManager = [[self alloc] initCommonTipPopoViewWithMessage:message];
        
        _commonTipPopoViewManager.frame = [UIScreen mainScreen].bounds;
//        _commonTipPopoViewManager.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        UIWindow *mainWindow = [UIApplication sharedApplication].windows[0];
        [mainWindow addSubview:_commonTipPopoViewManager];
    }
    return _commonTipPopoViewManager;
}

在iOS 13之前,弹窗可以通过UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;直接添加,但是在iOS 13之后,要注意这里一般可以这样写:UIWindow *mainWindow = [UIApplication sharedApplication].windows[0];

下面是实现这三种提示信息的弹窗的核心部分的代码:

static ASCommonTipPopoView *_commonTipPopoViewManager = nil;

核心代码部分:

- (instancetype)initCommonTipPopoViewWithMessage:(NSString *)message {
    
    if (self = [super init]) {
        
        [self setupUIWithMessage:message];
    }
    return self;
}

+ (instancetype)showCommonTipPopoViewWithMessage:(NSString *)message {
    
    if (_commonTipPopoViewManager == nil) {
        _commonTipPopoViewManager = [[self alloc] initCommonTipPopoViewWithMessage:message];
        
        _commonTipPopoViewManager.frame = [UIScreen mainScreen].bounds;
//        _commonTipPopoViewManager.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
        [mainWindow addSubview:_commonTipPopoViewManager];
    }
    return _commonTipPopoViewManager;
}

UI的创建和实现:

- (void)setupUIWithMessage:(NSString *)message {
    
    CGFloat viewW = [UIScreen mainScreen].bounds.size.width;
    CGFloat margin = 60;
    
    CGSize messageSize = [self sizeWithText:message font:[UIFont systemFontOfSize:13] maxSize:CGSizeMake(viewW - 2 * margin , MAXFLOAT)];
    
    CGFloat mainBGViewH = 55;
    if (messageSize.height > mainBGViewH) {
        mainBGViewH = messageSize.height;
    }
    
    [self addSubview:self.mainBGView];
    [self.mainBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(80 + messageSize.width, mainBGViewH));
    }];
    
    [self.mainBGView addSubview:self.iconImgView];
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mainBGView).offset(20);
        make.centerY.mas_equalTo(self.mainBGView);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    [self.mainBGView addSubview:self.messageLabel];
    self.messageLabel.text = message;
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(10);
        make.centerY.mas_equalTo(self.mainBGView);
        make.size.mas_equalTo(CGSizeMake(messageSize.width + 20, messageSize.height));
    }];
    
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf hiddenCommonTipPopoView];
    });
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenCommonTipPopoView)]];
}

下面是根据文字进行尺寸计算的逻辑实现;

/**
 计算文字尺寸
 
 @param text 需要计算文字的尺寸
 @param font 文字的字体
 @param maxSize 文字的最大尺寸
 @return 返回文字尺寸
 */
- (CGSize)sizeWithText: (NSString *)text font: (UIFont *)font maxSize:   (CGSize)maxSize{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

具体实现步骤:

1、在你的过程中导入头文件;

#import "ASCommonTipPopoView.h"

2、一行代码就可以实现你的需求,简单、实用。

[ASCommonTipPopoView showCommonTipPopoViewWithMessage:@"请输入您的昵称!"];

demo下载地址

上一篇 下一篇

猜你喜欢

热点阅读