iOS 自定制弹框

调用自定义弹框
CustomGeneralAlert *alert = [CustomGeneralAlert alertWithBgimage:@"alert_NoNetWork" Icon1:@"alert_cancle" icon2:@"alert_ture" Cancle:^{
            // 点击取消设置
        } continueBlock:^{
            // 点击去设置wifi
            // 跳到wifi设置界面
            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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值