iOS 自定制弹框

来源:互联网 发布:对淘宝客服的理解 编辑:程序博客网 时间:2024/06/06 20:13

调用自定义弹框

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
0 0
原创粉丝点击