这个控件写了很久,主要公司在升级ios7的时候发现以前的uialertview都无法使用了,所以抽空把系统的uialertview重新写了一下,对于已经入门1年多的我来说,或者大家来说已经没什么难度了,使用方法和uialertview一样,包括初始化,以及显示消失什么的都一样,使用范例如下:
初始化以及显示:
RAlertView* alert = [[RAlertView alloc] initWithTitle:_L(@"common_msg_title") message:_L(@"common_msg_del_confirm") delegate:self cancelButtonTitle:_L(@"common_cancel") otherButtonTitles:_L(@"common_confirm"),nil];
[alert show];
[alert release];
[alert dismissWithClickedButtonIndex:0 animated:NO];
代理方法:
- (void)alertView:(RAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
h文件:
@interface PointManager : NSObject
{
CGPoint startPoint;
CGPoint endPoint;
}
@property (nonatomic , assign)CGPoint startPoint;
@property (nonatomic , assign)CGPoint endPoint;
@end
@class RAlertView;
@protocol RAlertViewDelegate <NSObject>
@optional
- (void)alertView:(RAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
- (UIView *)popViewOnalertView:(RAlertView *)alertView;
- (void)didPresentAlertView:(RAlertView *)alertView;
- (void)willPresentAlertView:(RAlertView *)alertView;
@end
@interface RAlertView : UIAlertView{
UIImageView *backgroundView;
id<RAlertViewDelegate> m_delegate;
UIWindow *modelWindow; //警告框架构的window
NSMutableArray * pointArray;
float angle;
UIInterfaceOrientation lastOrtation;
CGAffineTransform m_transfrom;
UIViewController * ctrl;
BOOL keyBoradShow;
CGPoint ortionRect;
CGFloat m_height;
CGFloat m_width;
// UIView * popView;
}
@property(nonatomic,assign)id<RAlertViewDelegate> m_delegate;
@property(nonatomic,assign)UIInterfaceOrientation lastOrtation;
//初始化函数
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id )delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... ;
//设置背景
-(void)setBackground:(UIImage*)image;
//显示警告框
-(void)show;
//警告框消失
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
m文件:
#define getDeviceVersion [[[UIDevice currentDevice] systemVersion] intValue]
#define getRealScreenHeight [[UIScreen mainScreen] bounds].size.height
#define getRealScreenWidth [[UIScreen mainScreen] bounds].size.width
@implementation PointManager
@synthesize startPoint,endPoint;
@end
@interface RAlertView(private)
-(void)setButtonFrame:(NSInteger)index offY:(CGFloat)offsetY;
@end
@implementation RAlertView
@synthesize m_delegate;
@synthesize lastOrtation;
//初始化函数
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id )delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...{
if (getDeviceVersion < 7.0) {
self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
return self;
}
self=[super initWithFrame:CGRectZero];
if (self) {
//视图
self.backgroundColor=[UIColor clearColor];
pointArray = [[NSMutableArray alloc] init];
modelWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[modelWindow setBackgroundColor:[UIColor clearColor]];
modelWindow.windowLevel = UIWindowLevelStatusBar + 1;
modelWindow.hidden = YES;
m_delegate = delegate;
ctrl = [[UIViewController alloc] init];
UIWindow* keyWindow=[[[UIApplication sharedApplication] windows] lastObject];
if([keyWindow isKindOfClass:NSClassFromString(@"UITextEffectsWindow")]){
keyBoradShow = YES;
}
// iDMSSAppDelegate * appdelegate = (iDMSSAppDelegate *)[[UIApplication sharedApplication] delegate];
// UIView * view = [[appdelegate.appWindow subviews] objectAtIndex:0];
UIView * view = nil;
if ([[[[UIApplication sharedApplication] delegate].window subviews] count] > 0) {
view = [[[[UIApplication sharedApplication] delegate].window subviews] objectAtIndex:0];
}
if (view != nil) {
ctrl = [view valueForKey:@"viewDelegate"];
}
//数据
//set title
UIFont *titlefont = [UIFont boldSystemFontOfSize:18];
CGSize titlesize = [title sizeWithFont:titlefont constrainedToSize:CGSizeMake(240.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, titlesize.width+5, titlesize.height+5)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor=[UIColor blackColor];
titleLabel.font = titlefont;
titleLabel.numberOfLines = 0;
[titleLabel setTextAlignment:UITextAlignmentCenter];
titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
titleLabel.text = title;
//set message
UIFont *messagefont = [UIFont systemFontOfSize:16];
CGSize messagesize = [message sizeWithFont:messagefont constrainedToSize:CGSizeMake(260.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f+titlesize.height+8, messagesize.width+5, messagesize.height+5)];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textColor=[UIColor blackColor];
messageLabel.font = messagefont;
messageLabel.numberOfLines = 0;
[messageLabel setTextAlignment:UITextAlignmentCenter];
messageLabel.lineBreakMode = UILineBreakModeCharacterWrap;
messageLabel.text = message;
NSInteger index = 0;
if (cancelButtonTitle) {
UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:cancelBtn];
// [cancelBtn setBackgroundColor:[UIColor redColor]];
[cancelBtn setTitle:cancelButtonTitle forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
[cancelBtn setTitleColor:[UIColor colorWithRed:33.0/255.0f green:129.0/255.0f blue:247.0/255.0f alpha:1.0] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
cancelBtn.tag=100;
index++;
}
va_list args;
if (otherButtonTitles) {
UIButton *otherbutton = nil;
otherbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:otherbutton];
[otherbutton setTitle:otherButtonTitles forState:UIControlStateNormal];
[otherbutton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[otherbutton setTitleColor:[UIColor colorWithRed:33.0/255.0f green:129.0/255.0f blue:247.0/255.0f alpha:1.0] forState:UIControlStateNormal];
[otherbutton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
if (index == 0) {
otherbutton.tag = 100;
}else{
otherbutton.tag = index;
}
index++;
NSString *buttonTitle = nil;
va_start(args,otherButtonTitles);
while ((buttonTitle=va_arg(args, NSString*))) {
otherbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:otherbutton];
[otherbutton setTitle:buttonTitle forState:UIControlStateNormal];
[otherbutton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[otherbutton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[otherbutton setTitleColor:[UIColor colorWithRed:33.0/255.0f green:129.0/255.0f blue:247.0/255.0f alpha:1.0] forState:UIControlStateNormal];
otherbutton.tag = index;
index++;
}
va_end(args);
}
[self addSubview:titleLabel];
titleLabel.center=CGPointMake(140, titleLabel.center.y);
[titleLabel release];
[self addSubview:messageLabel];
messageLabel.center=CGPointMake(140, messageLabel.center.y);
[messageLabel release];
[self setButtonFrame:index offY:titlesize.height+messagesize.height];
[self setCenter:CGPointMake(modelWindow.bounds.size.width/2, modelWindow.bounds.size.height/2)];
m_height = self.frame.size.height;
m_width = self.frame.size.width;
// lastOrtation = UIInterfaceOrientationPortrait;
}
return self;
}
- (void)keyboardDidShow:(id)add
{
NSLog(@"hahaha!");
}
-(void)setButtonFrame:(NSInteger)index offY:(CGFloat)offsetY{
UIButton *button = nil;
if (index == 2) {
button = (UIButton*)[self viewWithTag:100];
[button setFrame:CGRectMake(0, offsetY+40, 139, 40)];
PointManager * point1 = [[PointManager alloc] init];
point1.startPoint = CGPointMake(0, offsetY + 40);
point1.endPoint = CGPointMake(280, offsetY + 40);
[pointArray addObject:point1];
[point1 release];
PointManager * point2 = [[PointManager alloc] init];
point2.startPoint = CGPointMake(140, offsetY + 40);
point2.endPoint = CGPointMake(140, offsetY + 80);
[pointArray addObject:point2];
[point2 release];
button = (UIButton*)[self viewWithTag:1];
[button setFrame:CGRectMake(141, offsetY+40, 139, 40)];
[self setNeedsDisplay];
[self setFrame:CGRectMake(0, 0, 280, offsetY+80)];
}else{
for (int i =0 ; i<index; i++) {
if (i== 0) {
button = (UIButton*)[self viewWithTag:100];
}else{
button = (UIButton*)[self viewWithTag:i];
}
if (i != index) {
PointManager * hon_point = [[PointManager alloc] init];
hon_point.startPoint = CGPointMake(0, offsetY+(i+1)*45);
hon_point.endPoint = CGPointMake(280, offsetY+(i+1)*45);
[pointArray addObject:hon_point];
}
[button setFrame:CGRectMake(0, offsetY+(i+1)*45 + 1, 280, 40)];
}
[self setNeedsDisplay];
if ((offsetY+45+45*index) < 80) {
[self setFrame:CGRectMake(0, 0, 280, 100)];
}else{
[self setFrame:CGRectMake(0, 0, 280, offsetY+45+45*index)];
}
}
}
- (void)drawRect:(CGRect)rect
{
if (getDeviceVersion >= 7.0) {
UIImage * image = [UIImage imageNamed:@"pop_bg.png"];
[image drawInRect:self.bounds];
for (int i = 0; i < [pointArray count]; i++) {
PointManager * point = [pointArray objectAtIndex:i];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 0.5);
CGContextMoveToPoint(context, point.startPoint.x, point.startPoint.y);
CGContextAddLineToPoint(context, point.endPoint.x, point.endPoint.y);
CGContextSetRGBStrokeColor(context, 157/255.0f, 161/255.0f, 160/255.0f, 1.0);
CGContextStrokePath(context);
}
}
}
- (void)setRalertOrtation:(UIInterfaceOrientation)ortain
{
if (ortain == UIInterfaceOrientationPortrait) {
angle = 0.0;
}else if (ortain == UIInterfaceOrientationPortraitUpsideDown){
angle = M_PI;
}else if (ortain == UIInterfaceOrientationLandscapeRight){
angle = M_PI/2;
}else{
angle = -M_PI/2;
}
self.transform = CGAffineTransformIdentity;
self.transform = CGAffineTransformMakeRotation(angle);
m_transfrom = self.transform;
}
//设置背景
-(void)setBackground:(UIImage*)image{
CGSize size = [image size];
CGFloat width=0,height=0;
//图片宽度较小
if (240-size.width>0) {
width = 240-size.width;
}
//图片高度不够
if (self.frame.size.height-size.height>0) {
height = self.frame.size.height-size.height;
}
[image stretchableImageWithLeftCapWidth:width topCapHeight:height];
[backgroundView setImage:image];
}
//显示警告框
-(void)show{
if (getDeviceVersion < 7.0) {
[super show];
}else{
if (m_delegate && [m_delegate respondsToSelector:@selector(didPresentAlertView:)]) {
[m_delegate willPresentAlertView:self];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyOrtainAnimationwithAmiation:) name:UIDeviceOrientationDidChangeNotification object:nil];
UIDevice * device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
if (modelWindow.subviews) {
for (id view in modelWindow.subviews) {
if ([view isKindOfClass:[UIView class] && view]) {
[view removeFromSuperview];
}
}
}
[modelWindow addSubview:self];
[modelWindow bringSubviewToFront:self];
modelWindow.hidden = NO;
[self setRalertOrtation:ctrl.interfaceOrientation];
self.transform = CGAffineTransformScale(m_transfrom, 0.1f, 0.1f);
[UIView animateWithDuration:0.15 animations:^{
// 放大 110%
self.transform = CGAffineTransformScale(m_transfrom, 1.1f, 1.1f);
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.15 animations:^{
// 返回 100%
self.transform = CGAffineTransformScale(m_transfrom, 1.0f, 1.0f);
if (keyBoradShow) {
[self keyboardDidShow];
}
}];
}];
}
// [self notifyOrtainAnimationwithAmiation:NO];
if (m_delegate && [m_delegate respondsToSelector:@selector(didPresentAlertView:)]) {
[m_delegate didPresentAlertView:self];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"interfaceOrientation"]) {
[self notifyOrtainAnimationwithAmiation:YES];
}
}
- (void)keyboardDidShow{
keyBoradShow = YES;
UIInterfaceOrientation ortain = ctrl.interfaceOrientation;
CGRect rect = self.frame;
UIWindow* keyWindow=[[[UIApplication sharedApplication] windows] lastObject];
CGRect keyboardRect = CGRectZero;
for (UIView *possibleKeyboard in [keyWindow subviews]) {
if([possibleKeyboard isKindOfClass:NSClassFromString(@"UIPeripheralHostView")] || [possibleKeyboard isKindOfClass:NSClassFromString(@"UIKeyboard")]){
keyboardRect = possibleKeyboard.frame;
break;
}
}
int height;
if (ortain == UIInterfaceOrientationPortrait) {
height = keyboardRect.size.height;
if (rect.origin.y + rect.size.height > getRealScreenHeight - height) {
ortionRect = CGPointMake(modelWindow.frame.size.width/2, modelWindow.frame.size.height - (height + 5+ m_height/2));
}else{
ortionRect = self.center;
}
}else if(ortain == UIInterfaceOrientationPortraitUpsideDown){
height = keyboardRect.size.height;
if (height > rect.origin.y) {
ortionRect = CGPointMake(modelWindow.frame.size.width/2, height + 5 + m_height/2);
}else{
ortionRect = self.center;
}
}else if(ortain == UIInterfaceOrientationLandscapeLeft){
height = keyboardRect.size.height;
if (height < rect.origin.x + rect.size.width) {
ortionRect = CGPointMake(modelWindow.frame.size.width - (height + 5 + m_height/2), modelWindow.frame.size.height/2);
}else{
ortionRect = self.center;
}
}else{
height = keyboardRect.size.height;
if (height > rect.origin.x) {
ortionRect = CGPointMake(height + 5 + m_height/2, modelWindow.frame.size.height/2);
}else{
ortionRect = self.center;
}
}
self.center = ortionRect;
}
//警告框消失
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated{
if (getDeviceVersion < 7.0) {
[super dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
if (self) {
if (animated) {
[UIView animateWithDuration:0.2f
animations:^{
self.transform = CGAffineTransformScale(m_transfrom, 0.1f, 0.1f);
}completion:^(BOOL finished){
modelWindow.hidden = YES;
[modelWindow release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// [self release];
}];
}else{
modelWindow.hidden = YES;
[modelWindow release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// [self release];
}
}
}
-(void)buttonClicked:(id)sender{
if (m_delegate&&[m_delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
{
NSInteger index = [sender tag];
if (index == 100) {
index = 0;
}
[m_delegate alertView:self clickedButtonAtIndex:index];
}
modelWindow.hidden = YES;
[modelWindow release];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismissWithClickedButtonIndex:animated:) object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// [self release];
}
-(void)dealloc
{
[pointArray release];
[backgroundView release];
[super dealloc];
}
- (void)notifyOrtainAnimationwithAmiation:(BOOL)amiation
{
if (getDeviceVersion >= 7.0) {
UIInterfaceOrientation ortain;
UIDeviceOrientation deviceortation = [UIDevice currentDevice].orientation;
if (deviceortation > 4) {
ortain = [UIApplication sharedApplication].statusBarOrientation;
}else{
ortain = deviceortation;
}
// UIInterfaceOrientation ortain = ctrl.interfaceOrientation;
#ifdef IOS6_COMPLIE
if ([ctrl respondsToSelector:@selector(shouldAutorotate)]) {
if (![ctrl shouldAutorotate]) {
return;
}
}
if ([ctrl respondsToSelector:@selector(supportedInterfaceOrientations)]) {
int orientation = [ctrl supportedInterfaceOrientations];
if (!((1 << ortain) & orientation)) {
return;
}
}
#else
if ([ctrl respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
BOOL aorientation = [ctrl shouldAutorotateToInterfaceOrientation:ortain];
if (!aorientation) {
return;
}
}
#endif
CGFloat newangle;
if (ortain == UIInterfaceOrientationPortrait) {
newangle = 0.0;
}else if (ortain == UIInterfaceOrientationPortraitUpsideDown){
newangle = M_PI;
}else if (ortain == UIInterfaceOrientationLandscapeRight){
newangle = M_PI/2;
}else{
newangle = -M_PI/2;
}
CGFloat tAngle = newangle - angle;
if (fabs(tAngle) > M_PI + 0.1) {
if (tAngle > 0) {
tAngle = fabs(tAngle) - M_PI * 2 ;
}else{
tAngle = M_PI * 2 - fabs(tAngle);
}
}
if (amiation) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.38 * tAngle/M_PI_2];
[UIView setAnimationDelegate:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.transform = CGAffineTransformRotate(m_transfrom, tAngle);
[UIView commitAnimations];
}else{
self.transform = CGAffineTransformRotate(m_transfrom, tAngle);
}
m_transfrom = self.transform;
angle = newangle;
lastOrtation = ortain;
}
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
keyBoradShow = YES;
UIInterfaceOrientation ortain = ctrl.interfaceOrientation;
CGRect rect = self.frame;
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height;
if (ortain == UIInterfaceOrientationPortrait) {
height = keyboardRect.size.height;
if (rect.origin.y + rect.size.height > getRealScreenHeight - height) {
ortionRect = CGPointMake(modelWindow.frame.size.width/2, modelWindow.frame.size.height - (height + 5+ m_height/2));
}else{
ortionRect = self.center;
}
}else if(ortain == UIInterfaceOrientationPortraitUpsideDown){
height = keyboardRect.size.height;
if (height > rect.origin.y) {
ortionRect = CGPointMake(modelWindow.frame.size.width/2, height + 5 + m_height/2);
}else{
ortionRect = self.center;
}
}else if(ortain == UIInterfaceOrientationLandscapeLeft){
height = keyboardRect.size.width;
if (height < rect.origin.x + rect.size.width) {
ortionRect = CGPointMake(modelWindow.frame.size.width - (height + 5 + m_height/2), modelWindow.frame.size.height/2);
}else{
ortionRect = self.center;
}
}else{
height = keyboardRect.size.width;
if (height > rect.origin.x) {
ortionRect = CGPointMake(height + 5 + m_height/2, modelWindow.frame.size.height/2);
}else{
ortionRect = self.center;
}
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.center = ortionRect;
[UIView commitAnimations];
// delegate.b_ortionRect = ortionRect;
}
- (void)keyboardWillHidden:(NSNotification *)aNotification
{
keyBoradShow = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.center = CGPointMake(modelWindow.bounds.size.width/2, modelWindow.bounds.size.height/2);
[UIView commitAnimations];
}
- (void)notifyOrtainAnimationwithAmiation
{
if (getDeviceVersion >= 7.0) {
[self notifyOrtainAnimationwithAmiation:YES];
}
}
第一次写,不知道怎么上传两个文件,所以就直接贴上源码了,知道的可以告诉下我。