iOS开发系列之常用自定义控件开发集—自定义对话框(UIAlertView)控件开发
来源:互联网 发布:虚拟主机系统php 编辑:程序博客网 时间:2024/05/05 07:25
在我们开发过程中经常需要弹出一些对话框AlertView,当然系统提供UIAlertView对话框类但子ios7.0以后系统的对话框内容显示方式无法改变了默认居中了然而我们实际需求有时候需要对话框内容左对齐或者右对齐,这个时候就需要我们自定义了,接下来我们就编写自定义的对话框。
WHC_AlertView.h头文件如下:
//// WHC_AlertView.h// WHC_AlertView//// Created by 吴海超 on 15/4/2.// Copyright (c) 2015年 吴海超. All rights reserved.//#import <UIKit/UIKit.h>@class WHC_AlertView;//单击代理@protocol WHC_AlertViewDelegate <NSObject>- (void)whcAlertView:(WHC_AlertView*)alertView index:(NSInteger)index;@end@interface WHC_AlertView : UIView//下面是现实对话框方法的多个版本接口+ (void)showAlert:(NSString*)msg vc:(UIViewController*)vc;+ (void)showAlert:(NSString*)msg vc:(UIViewController *)vc delegate:(id)delegate;+ (void)showAlert:(NSString*)title msg:(NSString*)msg vc:(UIViewController*)vc;+ (void)showAlert:(NSString*)title msg:(NSString*)msg btnTitles:(NSArray*)btnTitles vc:(UIViewController*)vc;+ (void)showAlert:(NSString *)title msg:(NSString *)msg btnTitles:(NSArray*)btnTitles vc:(UIViewController *)vc delegate:(id)delegate;+ (void)showAlert:(NSString *)title msg:(NSString *)msg btnTitles:(NSArray*)btnTitles alignment:(NSTextAlignment)alignment vc:(UIViewController *)vc;+ (void)showAlert:(NSString *)title msg:(NSString *)msg btnTitles:(NSArray*)btnTitles alignment:(NSTextAlignment)alignment vc:(UIViewController *)vc delegate:(id)delegate;@end
WHC_AlertView.m源文件如下:
//// WHC_AlertView.m// WHC_KeyBorad//// Created by 吴海超 on 15/4/2.// Copyright (c) 2015年 吴海超. All rights reserved.//#import "WHC_AlertView.h"#define KWHC_PADING (20.0) //显示对话框与屏幕边距#define KWHC_TITLE_FONT_SIZE (20.0) //标题字体尺寸#define KWHC_MSG_FONT_SIZE (16.0) //内容字体尺寸#define KWHC_TOP_HEIGHT (30.0) //内容与标题间距像素#define KWHC_BOTTOM_HEIGHT (40.0) //内容与底部按钮间距像素#define KWHC_LINE_WIDTH (0.5) //分隔线宽度#define KWHC_LINE_ALPHA (0.3) //分隔线宽度透明度#define KWHC_ANIMATION_DURING (0.2) //动画显示对话框周期#define KWHC_MSG_PADING (30.0) //内容显示边距#define KWHC_ALERTVIEW_ID (-100000) //对话框id#define KWHC_ALPHA_VIEW_ID (39939302) //阴影层id#define KWHC_TITLE_COLOR ([UIColor redColor]) //标题文字颜色#define KWHC_MSG_COLOR ([UIColor blackColor]) //内容文字颜色@interface WHC_AlertView (){ id<WHC_AlertViewDelegate> _delegate;}@end@implementation WHC_AlertView//创建分隔线- (UILabel*)createLine:(CGRect)frame{ UILabel * labLine = [[UILabel alloc]initWithFrame:frame]; labLine.backgroundColor = [UIColor blackColor]; return labLine;}//创建按钮- (UIButton*)createBtn:(CGRect)frame title:(NSString*)title{ UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = frame; btn.backgroundColor = [UIColor clearColor]; [btn setTitle:title forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; [btn addTarget:self action:@selector(clickWHCAlert:) forControlEvents:UIControlEventTouchUpInside]; return btn;}//创建主视图- (UIView *)createBaseBackView:(NSString*)title msg:(NSString*)msg btnTitles:(NSArray*)btnTitles titleColor:(UIColor*)titleColor titleFontSize:(CGFloat)titleFontSize msgColor:(UIColor*)msgColor msgFontSize:(CGFloat)msgFontSize withAlignment:(NSTextAlignment)textAlignment delegate:(id<WHC_AlertViewDelegate>)delegate{ _delegate = delegate; BOOL isTitle = NO; CGFloat backViewWidth = CGRectGetWidth([UIScreen mainScreen].bounds) - KWHC_PADING * 2.0; //兼容底版本#pragma clang diagnostic push#pragma clang diagnostic ignored"-Wdeprecated-declarations" CGSize msgSize = [msg sizeWithFont:[UIFont systemFontOfSize:KWHC_MSG_FONT_SIZE] constrainedToSize:CGSizeMake(backViewWidth - KWHC_PADING, MAXFLOAT)];#pragma clang diagnostic pop CGFloat backViewHeight = KWHC_BOTTOM_HEIGHT + msgSize.height + KWHC_MSG_PADING + KWHC_LINE_WIDTH; if(title != nil && title.length){ backViewHeight = KWHC_TOP_HEIGHT + KWHC_BOTTOM_HEIGHT + msgSize.height + KWHC_MSG_PADING + KWHC_LINE_WIDTH * 2.0; isTitle = YES; } UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, backViewWidth, backViewHeight)]; view.backgroundColor = [UIColor whiteColor]; view.layer.cornerRadius = 10.0; view.clipsToBounds = YES; view.tag = KWHC_ALERTVIEW_ID; CGFloat y = 0.0; if(isTitle){ UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, backViewWidth, KWHC_TOP_HEIGHT)]; titleLab.backgroundColor = [UIColor clearColor]; titleLab.text = title; titleLab.textAlignment = NSTextAlignmentCenter; titleLab.font = [UIFont boldSystemFontOfSize:titleFontSize]; titleLab.textColor = titleColor; [view addSubview:titleLab]; UILabel * lineLab = [self createLine:CGRectMake(0.0, KWHC_TOP_HEIGHT, backViewWidth, KWHC_LINE_WIDTH)]; [view addSubview:lineLab]; y = CGRectGetHeight(lineLab.frame) + CGRectGetMinY(lineLab.frame); } UILabel * msgLab = [[UILabel alloc]initWithFrame:CGRectMake(KWHC_PADING / 2.0,y , backViewWidth - KWHC_PADING, msgSize.height + KWHC_MSG_PADING)]; msgLab.textColor = msgColor; msgLab.text = msg; msgLab.font = [UIFont systemFontOfSize:msgFontSize]; msgLab.numberOfLines = 0; msgLab.textAlignment = textAlignment; y = CGRectGetHeight(msgLab.frame) + CGRectGetMinY(msgLab.frame); [view addSubview:msgLab]; BOOL isSLine = NO; if(btnTitles.count){ isSLine = YES; UILabel * lineLab2 = [self createLine:CGRectMake(0.0, y, backViewWidth, KWHC_LINE_WIDTH)]; [view addSubview:lineLab2]; y = CGRectGetHeight(lineLab2.frame) + CGRectGetMinY(lineLab2.frame); } NSInteger btnCount = btnTitles.count; CGFloat btnWidth = (backViewWidth - (btnCount - 1) * KWHC_LINE_WIDTH) / (CGFloat)btnCount; //创建按钮 for (NSInteger i = 0; i < btnCount; i++) { UIButton * btn = [self createBtn:CGRectMake(i * btnWidth + i * KWHC_LINE_WIDTH, y, btnWidth, KWHC_BOTTOM_HEIGHT) title:btnTitles[i]]; btn.tag = i; [view addSubview:btn]; if(i < btnCount - 1){ UILabel * lineLab = [self createLine:CGRectMake((i + 1) * btnWidth, y, KWHC_LINE_WIDTH, KWHC_BOTTOM_HEIGHT)]; [view addSubview:lineLab]; } } return view;}//按钮响应- (void)clickWHCAlert:(UIButton*)sender{ if(_delegate && [_delegate respondsToSelector:@selector(whcAlertView:index:)]){ [_delegate whcAlertView:self index:sender.tag]; } [self removeFromSuperview];}//3d效果- (CATransform3D)loadTransform3D:(CGFloat)z{ CATransform3D scale = CATransform3DIdentity; scale.m34 = -1.0 / 1000.0; CATransform3D transform = CATransform3DMakeTranslation(0.0, 0.0, z); return CATransform3DConcat(transform ,scale);}//显示对话框默认没有标题和按钮代理内容居中+ (void)showAlert:(NSString*)msg vc:(UIViewController*)vc{ [WHC_AlertView showAlert:nil msg:msg vc:vc];}//显示对话框默认没有标题和按钮内容居中+ (void)showAlert:(NSString*)msg vc:(UIViewController *)vc delegate:(id)delegate{ [WHC_AlertView showAlert:nil msg:msg btnTitles:nil vc:vc delegate:delegate];}//显示对话框默认没有代理和按钮内容居中+ (void)showAlert:(NSString*)title msg:(NSString*)msg vc:(UIViewController*)vc{ [WHC_AlertView showAlert:title msg:msg btnTitles:nil vc:vc];}//显示对话框默认没有代理内容居中+ (void)showAlert:(NSString*)title msg:(NSString*)msg btnTitles:(NSArray*)btnTitles vc:(UIViewController*)vc{ [WHC_AlertView showAlert:title msg:msg btnTitles:btnTitles alignment:NSTextAlignmentCenter vc:vc];}//显示对话框内容居中+ (void)showAlert:(NSString *)title msg:(NSString *)msg btnTitles:(NSArray*)btnTitles vc:(UIViewController *)vc delegate:(id)delegate{ [WHC_AlertView showAlert:title msg:msg btnTitles:btnTitles alignment:NSTextAlignmentCenter vc:vc delegate:delegate];}//显示对话框默认没有代理+ (void)showAlert:(NSString *)title msg:(NSString *)msg btnTitles:(NSArray*)btnTitles alignment:(NSTextAlignment)alignment vc:(UIViewController *)vc{ [WHC_AlertView showAlert:title msg:msg btnTitles:btnTitles alignment:alignment vc:vc delegate:nil]; }//显示对话框+ (void)showAlert:(NSString *)title msg:(NSString *)msg btnTitles:(NSArray*)btnTitles alignment:(NSTextAlignment)alignment vc:(UIViewController *)vc delegate:(id)delegate{ if(btnTitles == nil){ btnTitles = @[@"确定"]; } CGFloat screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds); CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds); WHC_AlertView * alertView = [[WHC_AlertView alloc]initWithFrame:vc.view.bounds]; alertView.backgroundColor = [UIColor clearColor]; [vc.view addSubview:alertView]; UIView * alphaView = [[UIView alloc]initWithFrame:alertView.bounds]; alphaView.tag = KWHC_ALPHA_VIEW_ID; alphaView.backgroundColor = [UIColor blackColor]; alphaView.alpha = 0.5; alphaView.userInteractionEnabled = NO; [alertView addSubview:alphaView]; UIView * view = [alertView createBaseBackView:title msg:msg btnTitles:btnTitles titleColor:KWHC_TITLE_COLOR titleFontSize:KWHC_TITLE_FONT_SIZE msgColor:KWHC_MSG_COLOR msgFontSize:KWHC_MSG_FONT_SIZE withAlignment:alignment delegate:delegate]; CGRect viewRc = view.frame; view.alpha = 0.95; view.frame = CGRectMake(KWHC_PADING, (screenHeight - CGRectGetHeight(viewRc)) / 2.0, CGRectGetWidth(viewRc), CGRectGetHeight(viewRc)); view.center = CGPointMake(screenWidth / 2.0, screenHeight / 2.0); view.layer.transform = [alertView loadTransform3D:-10000.0]; [alertView addSubview:view]; [UIView animateWithDuration:KWHC_ANIMATION_DURING animations:^{ view.layer.transform = [alertView loadTransform3D:100.0]; } completion:^(BOOL finished) { [UIView animateWithDuration:KWHC_ANIMATION_DURING / 2.0 animations:^{ view.layer.transform = [alertView loadTransform3D:0.0]; }]; }];}@end
WHC_AlertViewDemo下载
0 0
- iOS开发系列之常用自定义控件开发集—自定义对话框(UIAlertView)控件开发
- iOS开发系列之常用自定义控件开发集—自定义数字键盘控件开发
- iOS开发系列之常用自定义控件开发集—自定义UIPickerView控件开发2
- iOS开发系列之常用自定义控件开发集—自定义UIDatePicker控件开发
- iOS开发系列之常用自定义控件开发集—自定义组按钮控件开发
- iOS开发系列之常用自定义控件开发集—自定义下拉列表按钮控件开发
- iOS开发系列之常用自定义控件开发集—自定义状态栏消息提示控件开发
- iOS开发系列之常用自定义控件开发集—Android的Toast控件开发
- iOS开发系列之常用自定义控件开发集—进度条Loading控件开发
- iOS开发系列之常用自定义控件开发集—自动广告墙图片控件开发
- iOS开发系列之常用自定义控件开发集—自定义单选按钮或者多选按钮控件开发
- iOS开发系列之常用自定义控件开发集—自定义UITableViewCell侧滑菜单控件开发
- iOS开发系列之常用自定义控件开发集—自动3D广告墙图片控件开发
- iOS开发系列之常用自定义控件开发集—继承UIButton控件代码快速创建按钮开发
- IOS开发--自定义控件
- IOS开发--自定义控件
- IOS 自定义控件之UIAlertview
- iOS开发-ios7 UIAlertView自定义
- cocos2dx lua 代码加密
- C中运算符优先级
- 第二周-图书馆的书
- 肺癌晚期丈夫不敢死 只为让妻子落户深圳
- 图的邻接表(广度优先遍历,深度优先遍历,最小生成树(Kruskal算法))
- iOS开发系列之常用自定义控件开发集—自定义对话框(UIAlertView)控件开发
- android 工厂模式
- STL三大著名版本的趣闻
- A+B Problem
- Win8 Metro(C#)数字图像处理--2.53图像傅立叶变换
- iOS中GCD的那些坑
- 解决plupload-2.1.2,springmvc上传中文文件名乱码问题
- PyQt5学习笔记05----Qt Designer信号槽
- Fat Brother Learns to Multiply DFS + 剪枝