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
- iOS 自定制弹框
- iOS swift 自定制控件(数字键盘)
- iOS中如何自定制键盘呢?
- iOS 自定制搜索栏,保存历史搜索
- iOS tableview自定制以及scrollview的无卡顿定时滚动
- iOS 自定制TabBar(中间是一个大按钮)
- 自定制linux系统
- 自定制网页对话框
- ICMP 报文自定制
- 自定制模版
- 自定制代码段
- 自定制Application类
- 自定制ProgressView
- 定制iOS 7中的导航栏和状态栏(转自cocoaChina)
- IOS界面开发UItextField定制全局组件_自定文字左边距、图标
- iOS-UITableView 中自定制cell上UIButton互斥事件
- iOS-系统导航栏字体修改及自定制导航栏
- 【iOS学习笔记】界面开发UItextField定制全局组件_自定文字左边距、图标
- ES6 函数扩展
- 蓝牙核心技术与应用学习笔记
- 维基百科搬运 树 (数据结构)
- Thread子类中,不能使用Spring注解,变量为null
- 前端开发中如何解析json数据(二)
- iOS 自定制弹框
- 服务器延迟原因汇总
- @SerializedName注解
- cannot resolve symbol DefaultHttpClient等之类的提示
- c#中全局变量与局部变量的设置
- 欢迎使用CSDN-markdown编辑器
- Leetcode 8. String to Integer (atoi)
- 服务器延迟原因汇总
- 直接插入排序算法