调用自定义弹框
CustomGeneralAlert *alert = [CustomGeneralAlert alertWithBgimage:@"alert_NoNetWork" Icon1:@"alert_cancle" icon2:@"alert_ture" Cancle:^{
} continueBlock:^{
NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}];
[self.view addSubview:alert];
实现方法
.h
#import <UIKit/UIKit.h>
typedef void(^CancleBlock) ();
typedef void(^ContinueBlock)();
@interface CustomGeneralAlert : UIWindow
@property (strong,nonatomic) UIImageView *image;
@property (strong,nonatomic) UIButton *btn1;
@property (strong,nonatomic) UIButton *btn2;
@property (copy,nonatomic) CancleBlock cancleBlock;
@property (copy,nonatomic) ContinueBlock continueBlock;
@property (strong,nonatomic) UIView *bgView;
+ (instancetype)alertWithBgimage:(NSString *)bgImage Icon1:(NSString *)icon1 icon2:(NSString *)icon2 Cancle:(CancleBlock)cancle continueBlock:(ContinueBlock)sure;
.m
#import "CustomGeneralAlert.h"
#import "MacroDefinition.h"
#import "Masonry.h"
#define topSpace 130
#define leftSpace 80
@implementation CustomGeneralAlert
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.windowLevel = UIWindowLevelAlert;
[self makeKeyAndVisible];
self.backgroundColor = COLOR_HIDDEN;
[self customAlert];
[self showAction];
}
return self;
}
- (void)customAlert{
self.bgView = [UIView new];
[self addSubview:self.bgView];
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.bounds.origin.x);
make.centerY.mas_equalTo(self.bounds.origin.y);
make.width.mas_equalTo(alertWidth * kWIDTH);
make.height.mas_equalTo(alertHeight * kHEIGHT);
}];
_image = [UIImageView new];
[_bgView addSubview:_image];
[_image mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.bounds.origin.x);
make.centerY.mas_equalTo(self.bounds.origin.y);
make.width.mas_equalTo(alertWidth * kWIDTH);
make.height.mas_equalTo(alertHeight * kHEIGHT);
}];
_image.userInteractionEnabled = YES;
_image.image = [UIImage imageNamed:@"alert_NoWifi"];
_btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[_image addSubview:_btn1];
[_btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(topSpace * kHEIGHT);
make.left.mas_equalTo(leftSpace * kWIDTH);
make.width.mas_equalTo(85 * kWIDTH);
make.height.mas_equalTo(30 * kHEIGHT);
}];
[_btn1 setBackgroundImage:[UIImage imageNamed:@"alert_NoPlay"] forState:normal];
[_btn1 addTarget:self action:@selector(cancleClick:) forControlEvents:UIControlEventTouchUpInside];
_btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[_image addSubview:_btn2];
[_btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(topSpace * kHEIGHT);
make.left.mas_equalTo((leftSpace + 110) * kWIDTH);
make.width.mas_equalTo(85 * kWIDTH);
make.height.mas_equalTo(30 * kHEIGHT);
}];
[_btn2 setBackgroundImage:[UIImage imageNamed:@"alert_ContinuePlay"] forState:normal];
[_btn2 addTarget:self action:@selector(continueClick:) forControlEvents:UIControlEventTouchUpInside];
}
+ (instancetype)alertWithBgimage:(NSString *)bgImage Icon1:(NSString *)icon1 icon2:(NSString *)icon2 Cancle:(CancleBlock)cancle continueBlock:(ContinueBlock)sure{
CustomGeneralAlert *alert = [[CustomGeneralAlert alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
alert.image.image = [UIImage imageNamed:bgImage];
[alert.btn1 setBackgroundImage:[UIImage imageNamed:icon1] forState:normal];
[alert.btn2 setBackgroundImage:[UIImage imageNamed:icon2] forState:normal];
alert.cancleBlock = cancle;
alert.continueBlock = sure;
return alert;
}
- (void)cancleClick:(UIButton *)btn{
[self hiddenAction];
self.cancleBlock();
}
- (void)continueClick:(UIButton *)btn{
[self hiddenAction];
self.continueBlock();
}
- (void)showAction{
[UIView animateWithDuration:0.2 animations:^{
self.bgView.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
self.bgView.transform = CGAffineTransformIdentity;
}];
}];
}
- (void)hiddenAction{
[self deallocView];
}
- (void)deallocView{
[self.bgView removeFromSuperview];
[self removeFromSuperview];
self.hidden = YES;
}
@end