1、弹出屏蔽罩一般用在提示用户一些重要信息,和系统自带的UIAlertController起到一样的效果,只不过自定义屏蔽罩弹窗可以使内容更加多元化
2、代码如下
#import <UIKit/UIKit.h>
@class LZExchangeCouponView, BeanCoupon;
@protocol LZExchangeCouponViewDelegate <NSObject>
@optional
/**
* 确定按钮代理方法
*/
- (void)exchangeCouponViewDelegate:(LZExchangeCouponView *) exchangeCouponView addDefineButton:(UIButton *)defineButton;
/**
* 关闭按钮代理方法
*/
- (void)exchangeCouponViewDelegate:(LZExchangeCouponView *)exchangeCouponView addColseButton:(UIButton *)colseButton;
@end
@interface LZExchangeCouponView : UIView
/**
* 兑换券对象
*/
@property (nonatomic, strong) BeanCoupon *coupon;
/**
* 兑换券代理
*/
@property (nonatomic, weak) id<LZExchangeCouponViewDelegate> delegate;
@end
#import "LZExchangeCouponView.h"
#import "bean/Coupon.h"
@interface LZExchangeCouponView ()
/**
* 透明背景View
*/
@property (nonatomic, strong) UIView *bgView;
/**
* contentView
*/
@property (nonatomic, strong) UIView *couponBgView;
/**
* 背景图片
*/
@property (nonatomic, strong) UIImageView *bgImageView;
/**
* 兑换券图片
*/
@property (nonatomic, strong) UIImageView *exchangeImageView;
/**
* 兑换券提示
*/
@property (nonatomic, strong) UILabel *tsLabel;
/**
* 兑换天数
*/
@property (nonatomic, strong) UILabel *exchangeLabel;
/**
* 确定按钮
*/
@property (nonatomic, strong) UIButton *defineButton;
/**
* 关闭按钮
*/
@property (nonatomic, strong) UIButton *closeButton;
@end
@implementation LZExchangeCouponView
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//0、透明View
self.bgView = [[UIView alloc] init];
self.bgView.backgroundColor = [UIColor blackColor];
self.bgView.alpha = 0.5;
[self addSubview:self.bgView];
//1、contentView
self.couponBgView = [[UIView alloc] init];
// self.couponBgView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.couponBgView];
//2、背景图片
self.bgImageView = [[UIImageView alloc] init];
self.bgImageView.image = [UIImage imageNamed:@"coupon_bg"];
[self.couponBgView addSubview:self.bgImageView];
//3、兑换券图片
self.exchangeImageView = [[UIImageView alloc] init];
[self.couponBgView addSubview:self.exchangeImageView];
//4、兑换券提示
self.tsLabel = [[UILabel alloc] init];
self.tsLabel.text = @"是否现在使用该券";
self.tsLabel.textAlignment = NSTextAlignmentCenter;
self.tsLabel.font = [UIFont systemFontOfSize:13.0];
self.tsLabel.textColor = RWTitleColor(0, 0, 0);
[self.couponBgView addSubview:self.tsLabel];
//5、兑换天数
self.exchangeLabel = [[UILabel alloc] init];
// self.exchangeLabel.text = @"为您开通 7 天的VIP会员";
self.exchangeLabel.textAlignment = NSTextAlignmentCenter;
self.exchangeLabel.font = [UIFont systemFontOfSize:15.0];
self.exchangeLabel.textColor = RWTitleColor(107, 113, 113);
[self.couponBgView addSubview:self.exchangeLabel];
//6、确定按钮
self.defineButton = [[UIButton alloc] init];
[self.defineButton setTitle:@"确定使用" forState:UIControlStateNormal];
[self.defineButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.defineButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
self.defineButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.defineButton setBackgroundImage:[UIImage imageNamed:@"coupon_queding"] forState:UIControlStateNormal];
//确定按钮事件
[self.defineButton addTarget:self action:@selector(defineButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.couponBgView addSubview:self.defineButton];
//7、关闭按钮
self.closeButton = [[UIButton alloc] init];
[self.closeButton setImage:[UIImage imageNamed:@"coupon_close"] forState:UIControlStateNormal];
//关闭按钮事件
[self.closeButton addTarget:self action:@selector(closeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.couponBgView addSubview:self.closeButton];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
//防止在block中循环引用
__unsafe_unretained __typeof(self) weakSelf = self;
//0、透明背景View
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.mas_equalTo(0);
make.right.mas_equalTo(0);
make.bottom.mas_equalTo(0);
}];
//1、背景View
[self.couponBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(weakSelf.mas_centerX);
make.centerY.mas_equalTo(weakSelf.mas_centerY);
make.width.mas_equalTo(302);
make.height.mas_equalTo(247);
}];
//2、背景图片
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.mas_equalTo(0);
make.bottom.mas_equalTo(0);
make.right.mas_equalTo(0);
}];
//3、兑换券图片
[self.exchangeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.mas_equalTo(0);
make.right.mas_equalTo(0);
make.height.mas_equalTo(100);
}];
//4、兑换券提示
[self.tsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(weakSelf.exchangeImageView.mas_bottom).offset(20);
make.left.mas_equalTo(0);
make.right.mas_equalTo(0);
make.height.mas_equalTo(20);
}];
//5、兑换天数
[self.exchangeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(weakSelf.tsLabel.mas_bottom).offset(10);
make.left.mas_equalTo(0);
make.right.mas_equalTo(0);
make.height.mas_equalTo(30);
}];
//6、确定按钮
[self.defineButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-8);
make.left.mas_equalTo(30);
make.height.mas_equalTo(40);
make.right.mas_equalTo(-30);
}];
//7、关闭按钮
[self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(270);
make.top.mas_equalTo(-25);
make.height.mas_equalTo(50);
make.width.mas_equalTo(50);
}];
}
#pragma mark - 关闭按钮事件
/**
* 关闭按钮事件
*/
- (void)closeButtonClick:(UIButton *) closeButton {
NSLog(@"点击了关闭按钮");
if ([self.delegate respondsToSelector:@selector(exchangeCouponViewDelegate:addColseButton:)]) {
[self.delegate exchangeCouponViewDelegate:self addColseButton:closeButton];
}
}
#pragma mark - 确定按钮事件
/**
* 确定按钮事件
*/
- (void)defineButtonClick:(UIButton *) defineButton {
NSLog(@"点击了确定按钮");
if ([self.delegate respondsToSelector:@selector(exchangeCouponViewDelegate:addDefineButton:)]) {
[self.delegate exchangeCouponViewDelegate:self addDefineButton:defineButton];
}
}
- (void)setCoupon:(BeanCoupon *)coupon {
_coupon = coupon;
//设置兑换图片
[self.exchangeImageView sd_setImageWithURL:[NSURL URLWithString:[coupon getCouponSmallImgUrl]] placeholderImage:[UIImage imageNamed:@"imgMid"]];
//设置兑换天数
self.exchangeLabel.text = [NSString stringWithFormat:@"为您开通%@天的VIP会员", [coupon getVipDays]];
}