产品需求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
。

网友评论