自定义对话框(UIAlertView)控件开发

在我们开发过程中经常需要弹出一些对话框AlertView,当然系统提供UIAlertView对话框类但子ios7.0以后系统的对话框内容显示方式无法改变了默认居中了然而我们实际需求有时候需要对话框内容左对齐或者右对齐,这个时候就需要我们自定义了,接下来我们就编写自定义的对话框。 WHC_AlertView.h头文件如下:

<NSObject>- (void)whcAlertView:(WHC_AlertView*)alertView index:(NSInteger)index;: 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源文件如下:

(){id<WHC_AlertViewDelegate> _delegate;}//创建分隔线- (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;BOOLisTitle = NO;CGFloat backViewWidth = CGRectGetWidth([UIScreen mainScreen].bounds) – KWHC_PADING * 2.0;msgSize = [msg sizeWithFont:[UIFont systemFontOfSize:KWHC_MSG_FONT_SIZE] constrainedToSize:CGSizeMake(backViewWidth – KWHC_PADING, MAXFLOAT)];#pragma clang diagnostic popCGFloat 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下载

,在乎的应该是沿途的风景以及看风景的心情。

自定义对话框(UIAlertView)控件开发

相关文章:

你感兴趣的文章:

标签云: