iOS - 自己定义alertView,继承自UIView,能够加入子视图,标题图片+文字
这个更简单,能够看下demo https://github.com/DYLAN-LWB/WBAlertView
自己定义alertView,继承自UIView,能够在消息区域加入子视图:addCustomerSubview
// - 在须要alert的控制器调用 alertView show 方法
CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:@"提示"
message:@"dylan_lwb_"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles: nil];
// [alertView addCustomerSubview:myView];
[alertView show];
[alertView release]; // - 代理方法
-(void)alertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ }
-(void)alertViewClosed:(CustomAlertView *)alertView
{ }
-(void)willPresentCustomAlertView:(UIView *)alertView
{ }
// - 在项目里创建一个CustomAlertView分类, 把下边代码所有复制过去, 不要管干嘛的
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define RGBA_COLOR(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define kBCAlertViewPresentNotify @"kBCAlertViewPresentNotify" //alertview present notify #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @class CustomAlertView; @protocol CustomAlertViewDelegate <NSObject> @optional // - 代理方法
-(void)alertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-(void)alertViewClosed:(CustomAlertView *)alertView;
-(void)willPresentCustomAlertView:(UIView *)alertView; // - 隐藏有用类弹出键盘
- (void)hideCurrentKeyBoard; @end @interface CustomAlertView : UIView {
} @property (nonatomic, assign) id <CustomAlertViewDelegate> delegate;
@property (nonatomic, assign) BOOL isNeedCloseBtn; // - 左上角带叉叉按钮
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *message;
@property (nonatomic, retain) UIView *backView;
@property (nonatomic, retain) UIView *titleBackgroundView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *titleIcon;
@property (nonatomic, retain) NSMutableArray *customerViewsToBeAdd; - (id)initWithTitle:(NSString*)title message:(NSString*)message delegate:(id)delegate cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString*)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION; -(void)initTitle:(NSString*)title message:(NSString*)message delegate:(id)del cancelButtonTitle:(NSString*)cancelBtnTitle otherButtonTitles:(NSString*)otherBtnTitles, ...NS_REQUIRES_NIL_TERMINATION; - (void) show ; // - 在alertview中加入自己定义控件
- (void)addCustomerSubview:(UIView *)view; + (void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur; + (CustomAlertView *)defaultAlert; @end
#import "CustomAlertView.h"
@interface CustomAlertView() {
BOOL _isShow;
}
@property (nonatomic, retain) NSString *cancelButtonTitle;
@property (nonatomic, retain) NSMutableArray *otherButtonTitles; @end @implementation CustomAlertView static const CGFloat mWidth = 290;
static const CGFloat mHeight = 180;
static const CGFloat mMaxHeight = 250;
static const CGFloat mBtnHeight = 30;
static const CGFloat mBtnWidth = 110;
static const CGFloat mHeaderHeight = 40; + (CustomAlertView *)defaultAlert
{
static CustomAlertView *shareCenter = nil;
if (!shareCenter)
{
shareCenter = [[CustomAlertView alloc] init];
}
return shareCenter;
} - (NSMutableArray *)customerViewsToBeAdd
{
if (_customerViewsToBeAdd == nil)
{
_customerViewsToBeAdd = [[NSMutableArray alloc] init];
} return _customerViewsToBeAdd;
} - (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 20)];
if (self)
{
_isShow = NO;
}
return self;
} - (id)initWithTitle:(NSString*)title message:(NSString*)message delegate:(id)del cancelButtonTitle:(NSString*)cancelBtnTitle otherButtonTitles:(NSString*)otherBtnTitles, ...
{
self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 20)];
if (self)
{
self.delegate = del;
self.cancelButtonTitle = cancelBtnTitle;
self.isNeedCloseBtn = NO; if (!_otherButtonTitles)
{
va_list argList;
if (otherBtnTitles)
{
self.otherButtonTitles = [NSMutableArray array];
[self.otherButtonTitles addObject:otherBtnTitles];
}
va_start(argList, otherBtnTitles);
id arg;
while ((arg = va_arg(argList, id)))
{
[self.otherButtonTitles addObject:arg];
}
}
self.title = title;
self.message = message; }
return self;
} -(void)initTitle:(NSString*)title message:(NSString*)message delegate:(id)del cancelButtonTitle:(NSString*)cancelBtnTitle otherButtonTitles:(NSString*)otherBtnTitles, ...
{
if (self)
{
self.delegate = del;
self.cancelButtonTitle = cancelBtnTitle;
self.isNeedCloseBtn = NO; if (!_otherButtonTitles)
{
va_list argList;
if (otherBtnTitles)
{
self.otherButtonTitles = [NSMutableArray array];
[self.otherButtonTitles addObject:otherBtnTitles];
}
va_start(argList, otherBtnTitles);
id arg;
while ((arg = va_arg(argList, id)))
{
[self.otherButtonTitles addObject:arg];
}
}
self.title = title;
self.message = message;
}
} - (void) layoutSubviews
{
[super layoutSubviews]; for (UIView *view in [self subviews])
{
[view removeFromSuperview];
} UIView *bgView = [[UIView alloc] initWithFrame:self.frame];
[bgView setBackgroundColor:[UIColor blackColor]];
[bgView setAlpha:0.4];
[self addSubview:bgView];
[bgView release]; if (!_backView)
{
_backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mWidth, mHeight)];
_backView.opaque = NO;
_backView.backgroundColor = [UIColor whiteColor];
_backView.layer.shadowOffset = CGSizeMake(1, 1);
_backView.layer.shadowRadius = 2.0;
_backView.layer.shadowColor = [UIColor grayColor].CGColor;
_backView.layer.shadowOpacity = 0.8;
[_backView.layer setMasksToBounds:NO];
} // - 设置头部显示区域
// - 设置标题背景
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mWidth, mHeaderHeight)];
titleView.backgroundColor = RGBA_COLOR(36, 193, 64, 1); CGSize titleSize = CGSizeZero;
if (self.title && [self.title length] > 0)
{
titleSize = [self.title sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:18.0f]];
}
if (titleSize.width > 0)
{
// - 标题图片
CGFloat startX = (titleView.frame.size.width - 40 - titleSize.width) / 2;
UIImageView *titleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xxxxx.png"]];
titleImage.frame = CGRectMake(startX, (titleView.frame.size.height - 30) / 2, 30, 30);
self.titleIcon = titleImage; [titleView addSubview:titleImage];
[titleImage release];
startX += 40; // - 标题
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(startX, (titleView.frame.size.height - 20) / 2, titleSize.width, 20)]; //- 标题太长的话须要处理
if (titleLabel.frame.size.width > 250)
{
titleLabel.frame = CGRectMake(50, (titleView.frame.size.height - 20) / 2, mWidth - 60, 20); titleImage.frame = CGRectMake(5, (titleView.frame.size.height - 30) / 2, 30, 30); } titleLabel.text = self.title;
titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel = titleLabel; [titleView addSubview:titleLabel];
[titleLabel release];
}else
{
// - 标题图片
UIImageView *titleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xxxxx.png"]];
titleImage.frame = CGRectMake((titleView.frame.size.width - 30) / 2, (titleView.frame.size.height - 30) / 2, 30, 30);
[titleView addSubview:titleImage];
[titleImage release];
} if (self.isNeedCloseBtn)
{
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(mWidth-mHeaderHeight, 0, mHeaderHeight, mHeaderHeight)];
btn.backgroundColor = [UIColor clearColor];
[btn setImage:[UIImage imageNamed:@"btn_close_alertview.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
[btn release];
} [_backView addSubview:titleView];
self.titleBackgroundView = titleView;
[titleView release]; // - 设置消息显示区域
UIScrollView *msgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, mHeaderHeight, mWidth, (mHeight-mHeaderHeight * 2))]; // - 设置背景颜色
msgView.backgroundColor = [UIColor whiteColor]; // - 内容
CGSize messageSize = CGSizeZero;
if (self.message && [self.message length]>0)
{
messageSize = [self.message sizeWithFont:[UIFont systemFontOfSize:16.0f] constrainedToSize:CGSizeMake(mWidth-20, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; if (messageSize.width > 0)
{
UILabel *msgLabel = [[UILabel alloc] init];
msgLabel.font = [UIFont systemFontOfSize:16.0f];
msgLabel.text = self.message;
msgLabel.numberOfLines = 0;
msgLabel.textAlignment = NSTextAlignmentCenter;
msgLabel.backgroundColor = [UIColor clearColor];
msgLabel.frame = CGRectMake(10, ((mHeight - mHeaderHeight * 2) - messageSize.height) / 2, mWidth-20, messageSize.height + 5);
if(messageSize.height > mMaxHeight)
{
msgView.frame = CGRectMake(msgView.frame.origin.x, msgView.frame.origin.y, msgView.frame.size.width, mMaxHeight + 25);
_backView.frame = CGRectMake(0, 0, mWidth, mHeaderHeight * 2 + msgView.frame.size.height);
msgLabel.textAlignment = NSTextAlignmentLeft;
msgLabel.frame = CGRectMake(10, 10, mWidth - 20, messageSize.height);
msgView.contentSize = CGSizeMake(msgView.frame.size.width, msgLabel.frame.size.height + 20);
}
else if (messageSize.height > (mHeight-mHeaderHeight * 2) - 10)
{
msgView.frame = CGRectMake(msgView.frame.origin.x, msgView.frame.origin.y, msgView.frame.size.width, messageSize.height + 25);
_backView.frame = CGRectMake(0, 0, mWidth, mHeaderHeight * 2 + msgView.frame.size.height);
msgLabel.frame = CGRectMake(10, 10, mWidth - 20, messageSize.height + 5);
}
[msgView addSubview:msgLabel];
[msgLabel release];
[_backView addSubview:msgView];
}
}else{
if (self.customerViewsToBeAdd && [self.customerViewsToBeAdd count] > 0)
{
CGFloat startY = 0;
for (UIView *subView in self.customerViewsToBeAdd)
{
CGRect rect = subView.frame;
rect.origin.y = startY;
subView.frame = rect;
[msgView addSubview:subView];
startY += rect.size.height;
}
msgView.frame = CGRectMake(0, mHeaderHeight, mWidth, startY);
}
[_backView addSubview:msgView];
_backView.frame = CGRectMake(0, 0, mWidth, msgView.frame.size.height + mHeaderHeight * 2 +20);
}
[msgView release]; // - 设置button显示区域
if (_otherButtonTitles != nil || _cancelButtonTitle != nil)
{
UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, _backView.frame.size.height-mHeaderHeight, mWidth, mHeaderHeight)]; // - 假设仅仅显示一个button,须要计算button的显示大小
if (_otherButtonTitles == nil || _cancelButtonTitle == nil)
{
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((_backView.frame.size.width-mBtnWidth) / 2, 0, mBtnWidth, mBtnHeight)];
btn.backgroundColor = RGBA_COLOR(36, 193, 64, 1);
btn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0f];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
if (_otherButtonTitles == nil && _cancelButtonTitle != nil)
{
[btn setTitle:_cancelButtonTitle forState:UIControlStateNormal];
} else
{
[btn setTitle:[_otherButtonTitles objectAtIndex:0] forState:UIControlStateNormal];
}
btn.tag = 0;
btn.layer.cornerRadius = 5;
[btnView addSubview:btn];
[btn release];
}
else if(_otherButtonTitles && [_otherButtonTitles count] == 1)
{
// - 显示两个button
// - 设置确定button的相关属性
CGFloat startX = (_backView.frame.size.width-mBtnWidth*2-20)/2;
UIButton *otherBtn = [[UIButton alloc]initWithFrame:CGRectMake(startX, 0, mBtnWidth, mBtnHeight)];
otherBtn.backgroundColor = RGBA_COLOR(36, 193, 64, 1);
otherBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0f];
otherBtn.titleLabel.textColor = [UIColor whiteColor];
[otherBtn setTitle:[_otherButtonTitles objectAtIndex:0] forState:UIControlStateNormal];
otherBtn.layer.cornerRadius = 5;
[otherBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
otherBtn.tag = 0;
[btnView addSubview:otherBtn];
[otherBtn release];
startX += mBtnWidth+20; // - 设置取消button的相关属性
UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(startX, 0, mBtnWidth, mBtnHeight)];
cancelBtn.backgroundColor = RGBA_COLOR(36, 193, 64, 1);
cancelBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0f];
cancelBtn.titleLabel.textColor = [UIColor whiteColor];
[cancelBtn setTitle:_cancelButtonTitle forState:UIControlStateNormal];
cancelBtn.layer.cornerRadius = 5;
[cancelBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
cancelBtn.tag = 1;
[btnView addSubview:cancelBtn];
[cancelBtn release];
}
[_backView addSubview: btnView];
[btnView release];
}
else
{
CGRect rc = _backView.frame;
rc.size.height -= mHeaderHeight;
_backView.frame = rc;
} UIControl *touchView = [[UIControl alloc] initWithFrame:self.frame];
[touchView addTarget:self action:@selector(touchViewClickDown) forControlEvents:UIControlEventTouchDown];
touchView.frame = self.frame;
[self addSubview:touchView];
[touchView release];
_backView.center = self.center; [self addSubview:_backView]; if (!_isShow)
[CustomAlertView exChangeOut:_backView dur:0.5];
if (self.delegate)
{
if ([self.delegate respondsToSelector:@selector(willPresentCustomAlertView:)])
{
[self.delegate willPresentCustomAlertView:self];
} [[NSNotificationCenter defaultCenter] postNotificationName:kBCAlertViewPresentNotify object:nil userInfo:nil];
}
}
- (void)touchViewClickDown
{
if (self.delegate)
{
if ([self.delegate respondsToSelector:@selector(hideCurrentKeyBoard)])
{
[self.delegate hideCurrentKeyBoard];
}
}
} // - 在消息区域设置自己定义控件
- (void)addCustomerSubview:(UIView *)view
{
[self.customerViewsToBeAdd addObject:view];
} - (void)buttonClicked:(id)sender
{
UIButton *btn = (UIButton *) sender;
_isShow = NO;
if (btn)
{
if (self.delegate && [self.delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
{
[self.delegate alertView:self clickedButtonAtIndex:btn.tag];
}
[self removeFromSuperview];
}
} - (void)closeBtnClicked:(id)sender
{
_isShow = NO;
if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewClosed:)])
{
[self.delegate alertViewClosed:self];
}
[self removeFromSuperview];
} - (void)show
{
[self layoutSubviews];
if (!_isShow)
[[[UIApplication sharedApplication].delegate window] addSubview:self];
_isShow = YES;
} - (void)dealloc
{
[_backView release];
[_customerViewsToBeAdd release];
self.titleLabel = nil;
self.titleBackgroundView = nil;
self.titleIcon = nil; [super dealloc];
} // - alertview弹出动画
+ (void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur
{
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.duration = dur;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; NSMutableArray *values = [NSMutableArray array]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; animation.values = values;
animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"]; [changeOutView.layer addAnimation:animation forKey:nil];
} @end
iOS - 自己定义alertView,继承自UIView,能够加入子视图,标题图片+文字的更多相关文章
- iOS开发——UI篇OC篇&UIView/UIWindow/UIScreen/CALayer
UIView/UIWindow/UIScreen/CALayer 1.UIScreen可以获取设备屏幕的大小. 1 2 3 4 5 6 7 // 整个屏幕的大小 {{0, 0}, {320, 480} ...
- Spring IOC 之Bean定义的继承
一个Bean的定义可以包含大量的配置信息,包括构造器参数.属性值以及容器规范信息,比如初始化方法.静态工厂方法名字等等.一子bean的定义可以从父bean的定义中继承配置数据信息.子bean定义可以覆 ...
- iOS学习笔记(2)— UIView用户事件响应
UIView除了负责展示内容给用户外还负责响应用户事件.本章主要介绍UIView用户交互相关的属性和方法. 1.交互相关的属性 userInteractionEnabled 默认是YES ,如果设置为 ...
- iOS学习笔记(1)— UIView 渲染和内容管理
iOS中应用程序基本上都是基于MVC模式开发的.UIView就是模型-视图-控制器中的视图,在iOS终端上看到的.摸到的都是UIView. UIView在屏幕上定义了一个矩形区域和管理区域内容的接口. ...
- iOS开发——自定义AlertView
自定义的AlertView,可以选择出现的动画方式,正文信息高度自动变化,特意做了几个可以对比.没啥难点,直接上代码,一看就懂. 1.在YYTAlertView.h文件中 // // YYTAler ...
- iOS 11开发教程(十二)iOS11应用视图始祖——UIView
iOS 11开发教程(十二)iOS11应用视图始祖——UIView 在Swift中,NSObject是所有类的根类.同样在UIKit框架(UIKit框架为iOS应用程序提供界面对象和控制器)中,也存在 ...
- iOS开发UI篇—核心动画(UIView封装动画)
iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...
- iOS 杂笔-20(UIView和CALayer的区别与联系)
iOS 杂笔-20(UIView和CALayer的区别与联系) 每个 UIView 内部都有一个 CALayer 在背后提供内容的绘制和显示,并且 UIView 的尺寸样式都由内部的 Layer 所提 ...
- iOS用心学 UI基础之UIView
一.引入UI 在实际开发中,基本的流程大致如下图所示: UI(User Interface)作为最基本的要点,也是非常重要的一部分,UI界面的美观直接决定着着用户的体验,苹果官方给开发中提供了非常强大 ...
随机推荐
- Spring MVC中 提交表单报错400
背景: 在写SpringMVC表单提交的代码的时,在最后点击提交的时候总是会出现400的错误 原因: 主要原因就是表单提交的数据和对应实体类的属性无法完全匹配 解决方案: 查看我们提交的数据是否完全和 ...
- [luogu] P4105 [HEOI2014]南园满地堆轻絮 (贪心)
P4105 [HEOI2014]南园满地堆轻絮 题目描述 小 Z 是 ZRP(Zombies' Republic of Poetry,僵尸诗歌共和国)的一名诗歌爱好者,最近 他研究起了诗词音律的问题. ...
- VUE:class与style强制绑定
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- WinServer-授权规则
授权规则: 使用谓词可以限制网站只能使用某一种请求 来自为知笔记(Wiz)
- 洛谷 P1617 爱与愁的一千个伤心的理由
P1617 爱与愁的一千个伤心的理由 题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第二章. 经历了心痛后,爱与愁大神不行了. 题目描述 ...
- QMutex“A mutex must be unlocked in the same thread that locked it”解决(在run里创建对象是不二法宝)
多线程时出现如下警告信息: A mutex must be unlocked in the same thread that locked it: 原因可能有二: 1.创建QMutex不在当前线程: ...
- iOS (封装)一句话调用系统的alertView和alertController
前言: 本文仅作参考存留,请用新版封装:iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用 UIAlertController是iOS8.0之后出来的新方法,其将 ...
- elasticsearch如何安全重启
elasticsearch如何安全重启节点 问题: elasticsearch集群,有时候可能需要修改配置,增加硬盘,扩展内存等操作,需要对节点进行维护升级.但是业务不能停,如果直接kill掉节 点, ...
- nyoj--8--一种排序(排序,水题)
一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...
- asp.net mvc5 文件下载上传
下载:是通过点击a标签直接下载的方式,没有其他任何要求,在服务器上存在实体文件,不需要请求后台控制层 前段js: <a id="NF-DownLoad" authorize= ...