美文网首页ios开发指南iOS学习开发恩美第二个APP项目
iOS开发 | 封装类安卓的toast及图文toast

iOS开发 | 封装类安卓的toast及图文toast

作者: 无夜之星辰 | 来源:发表于2017-06-07 17:45 被阅读504次
允儿

产品需求UI图如下:

需求

做出的效果:

封装好的

思路:

AppDelegate的window上按需添加自定义view即可。

代码:

根据模块所在位置封装了一个类,名为CQPointHUD

.h文件:

#import <UIKit/UIKit.h>

@interface CQPointHUD : UIView

/** 纯文本toast提示 */
+ (void)showToastWithMessage:(NSString *)message;

/** 图文toast提示 */
+ (void)showToastWithMessage:(NSString *)message image:(NSString *)imageName;

@end

.m文件:

#import "CQPointHUD.h"
#import <Masonry.h>

@implementation CQPointHUD

/** 纯文本toast提示 */
+ (void)showToastWithMessage:(NSString *)message{
    // 背景view
    UIView *bgView = [[UIView alloc]init];
    [[UIApplication sharedApplication].keyWindow addSubview:bgView];
    bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.9];
    bgView.layer.cornerRadius = 5;
    
    // label
    UILabel *label = [[UILabel alloc]init];
    label.text = message;
    [bgView addSubview:label];
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:15];
    
    // 设置背景view的约束
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(bgView.superview);
        make.top.left.mas_equalTo(label).mas_offset(-20);
        make.bottom.right.mas_equalTo(label).mas_offset(20);
    }];
    
    // 设置label的约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_lessThanOrEqualTo(140);
        make.center.mas_equalTo(label.superview);
    }];
    
    // 2秒后移除toast
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.5 animations:^{
            bgView.alpha = 0;
        } completion:^(BOOL finished) {
            [bgView removeFromSuperview];
        }];
    });
}

/** 图文toast提示 */
+ (void)showToastWithMessage:(NSString *)message image:(NSString *)imageName{
    // 背景view
    UIView *bgView = [[UIView alloc]init];
    [[UIApplication sharedApplication].keyWindow addSubview:bgView];
    bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.9];
    bgView.layer.cornerRadius = 5;
    
    // 图片
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [bgView addSubview:imageView];
    
    // label
    UILabel *label = [[UILabel alloc]init];
    label.text = message;
    [bgView addSubview:label];
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:22];
    
    // 设置背景view的约束
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(bgView.superview);
        make.width.mas_equalTo(150);
    }];
    
    // 设置imageView的约束
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgView);
        make.top.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(34, 34));
    }];
    
    // 设置label的约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_lessThanOrEqualTo(130);
        make.centerX.mas_equalTo(label.superview);
        make.top.mas_equalTo(imageView.mas_bottom).mas_offset(20);
        make.bottom.mas_offset(-18);
    }];
    
    // 2秒后移除toast
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.5 animations:^{
            bgView.alpha = 0;
        } completion:^(BOOL finished) {
            [bgView removeFromSuperview];
        }];
    });
}

@end

直接使用

// 纯文本toast
- (void)showToast{
    [CQPointHUD showToastWithMessage:@"您还未达到相应积分\n无法兑换商品"];
}

// 图文toast
- (void)showImageToast{
    [CQPointHUD showToastWithMessage:@"兑换成功" image:@"sign"];
}

相关问题:

1. 如何设置label的内边距?

可以将label放在一个view上通过设置它们两个的约束间接达到设置label内边距的目的。

2. 如何让label宽度自适应但又不超过某一个值?

设置宽的约束mas_lessThanOrEqualTo

运动达人.gif

Demo

GitHub

相关文章

网友评论

本文标题:iOS开发 | 封装类安卓的toast及图文toast

本文链接:https://www.haomeiwen.com/subject/melafxtx.html