眾所周知,當偉大的iOS7系統發布后,表揚的一堆、謾罵的也一片,而對於我們程序員來說最關心的莫過於低版本系統上的程序在搞版本系統上的兼容性問題了。
在iOS6.1幾之前,當我們想要做一些提醒用戶或臨時獲取一些數據時,通常會彈出一個模態試圖,給予用戶提醒,而最常見的做法莫過於直接用UIAlertView添加控件或繼承UIAlertView,然后添加自己想要的控件,如:在執行網絡連接 下載等耗時任務時我們會彈出一個view 然后顯示一個指示器,具體做法:
- (IBAction)showTraditionAlert:(id)sender { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"正在下載....." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; alertView.delegate = self; [alertView show]; } #pragma mark -- UIAlertViewDelegate //實現代理增加網絡指示器 - (void)willPresentAlertView:(UIAlertView *)alertView;{ indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; indicator.frame = CGRectMake(110, 20, 50, 50); [alertView addSubview:indicator]; [indicator startAnimating]; [indicator release]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ [indicator stopAnimating]; }
但上面的做法到了iOS7上就沒有用了 ,你自己所添加的控件根本顯示不出來,也就是說在iOS7上不允許我們更改系統的UIAlertView了(至少目前是這樣2013/07/18 beta3版本),我想大家肯定也遇到了這樣的問題,那現在改怎么半呢? 可以采用UIWindow的方式實現具體做法網上很多,我比較懶 隨便寫了點
// // CustomizedAlertAnimation.h // AlertAnimationDemo // // Created by PSH_Chen_Tao on 7/18/13. // Copyright (c) 2013 wolfman. All rights reserved. // //這個類主要時用來對指定的view進行動畫,,動畫類似UIAlertView的出現和消失 #import <Foundation/Foundation.h> @protocol CustomizedAlertAnimationDelegate; @interface CustomizedAlertAnimation : NSObject @property(strong,nonatomic)UIView *view; @property(assign,nonatomic)id<CustomizedAlertAnimationDelegate> delegate; -(id)customizedAlertAnimationWithUIview:(UIView *)v; -(void)showAlertAnimation; -(void)dismissAlertAnimation; @end @protocol CustomizedAlertAnimationDelegate -(void)showCustomizedAlertAnimationIsOverWithUIView:(UIView *)v; -(void)dismissCustomizedAlertAnimationIsOverWithUIView:(UIView *)v; @end
// // CustomizedAlertAnimation.m // AlertAnimationDemo // // Created by PSH_Chen_Tao on 7/18/13. // Copyright (c) 2013 wolfman. All rights reserved. // #import "CustomizedAlertAnimation.h" static CGFloat kTransitionDuration = 0.3; @implementation CustomizedAlertAnimation @synthesize view; @synthesize delegate; -(void)dealloc{ if (delegate) { delegate = nil; } [view release]; view = nil; [super dealloc]; } -(id)customizedAlertAnimationWithUIview:(UIView *)v{ if (self=[super init]) { view = v; } return self; } //get the transform of view based on the orientation of device. -(CGAffineTransform)transformForOrientation{ CGAffineTransform transform ; UIInterfaceOrientation orientation = [[UIApplication sharedApplication ]statusBarOrientation]; switch (orientation) { case UIInterfaceOrientationLandscapeLeft: transform = CGAffineTransformMakeRotation(M_PI*1.5); break; case UIInterfaceOrientationLandscapeRight: transform = CGAffineTransformMakeRotation(M_PI/2); break;
//這里寫錯了,感謝 阿秉 提出問題,當為倒置方向時才應該旋轉 //case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(-M_PI); break; default: transform = CGAffineTransformIdentity; break; } return transform; } // begin the animation -(void)showAlertAnimation{ view.transform = CGAffineTransformScale([self transformForOrientation], 0.001, 0.001); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/1.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(firstBouncesDidStop)]; view.transform = CGAffineTransformScale([self transformForOrientation], 1.1, 1.1); [UIView commitAnimations]; } -(void)dismissAlertAnimation{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; view.alpha = 0; [UIView setAnimationDidStopSelector:@selector(dismissAlertAnimationDidStoped)]; [UIView commitAnimations]; } #pragma mark -- UIViewAnimation delegate -(void)firstBouncesDidStop{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(secondBouncesDidStop)]; view.transform = CGAffineTransformScale([self transformForOrientation], 0.9, 0.9); [UIView commitAnimations]; } -(void)secondBouncesDidStop{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:kTransitionDuration/2]; view.transform = [self transformForOrientation]; [UIView commitAnimations]; //You can do somethings at the end of animation [self.delegate showCustomizedAlertAnimationIsOverWithUIView:view]; } -(void)dismissAlertAnimationDidStoped{ [self.delegate dismissCustomizedAlertAnimationIsOverWithUIView:view]; } @end
// // ; // AlertDemo // // Created by PSH_Chen_Tao on 7/19/13. // Copyright (c) 2013 wolfman. All rights reserved. // //自定義的 alert view 類 #import <Foundation/Foundation.h> #import "CustomizedAlertAnimation.h" @protocol CustomeAlertViewDelegate ; @interface CustomeAlertView : UIWindow <CustomizedAlertAnimationDelegate> @property(strong,nonatomic)UIView *myView; @property(strong,nonatomic)UIActivityIndicatorView *activityIndicator; @property(strong,nonatomic)CustomizedAlertAnimation *animation; @property(assign,nonatomic)id<CustomeAlertViewDelegate> delegate; -(void)show; @end @protocol CustomeAlertViewDelegate -(void)CustomeAlertViewDismiss:(CustomeAlertView *) alertView; @end
// // CustomeAlertView.m // AlertDemo // // Created by PSH_Chen_Tao on 7/19/13. // Copyright (c) 2013 wolfman. All rights reserved. // #import "CustomeAlertView.h" @implementation CustomeAlertView @synthesize myView; @synthesize activityIndicator; @synthesize animation; @synthesize delegate; -(id)init{ if (self=[super init]) { self.frame = [[UIScreen mainScreen] bounds]; self.backgroundColor = [UIColor clearColor]; //UIWindow的層級 總共有三種 self.windowLevel = UIWindowLevelAlert; myView = [[UIView alloc]initWithFrame:CGRectMake(30, 140, 260, 200)]; UIButton *okButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [okButton setBackgroundImage:[UIImage imageNamed:@"alert-view-ok-button"] forState:UIControlStateNormal]; [okButton addTarget:self action:@selector(pressoKButton:) forControlEvents:UIControlEventTouchUpInside]; okButton.frame = CGRectMake(90, 130, 80, 40); [myView addSubview:okButton]; // [okButton release]; activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(105, 75, 50, 50)]; activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; [myView addSubview:activityIndicator]; // [activityIndicator release]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:myView.bounds]; [imageView setImage:[[UIImage imageNamed:@"alert-view-bg-portrait"] stretchableImageWithLeftCapWidth:100 topCapHeight:30]]; [myView insertSubview:imageView atIndex:0]; [imageView release]; animation = [[CustomizedAlertAnimation alloc]customizedAlertAnimationWithUIview:myView]; animation.delegate = self; [self addSubview:myView]; [myView release]; } return self; } -(void)show{ [self makeKeyAndVisible]; [animation showAlertAnimation]; } -(void)dismiss{ [self resignKeyWindow]; [animation dismissAlertAnimation]; } -(void) pressoKButton:(id)sender{ [self dismiss]; } #pragma mark -- CustomizedAlertAnimationDelegate //自定義的alert view出現動畫結束后調用 -(void)showCustomizedAlertAnimationIsOverWithUIView:(UIView *)v{ NSLog(@"showCustomizedAlertAnimationIsOverWithUIView"); [activityIndicator startAnimating]; } //自定義的alert view消失動畫結束后調用 -(void)dismissCustomizedAlertAnimationIsOverWithUIView:(UIView *)v{ NSLog(@"dismissCustomizedAlertAnimationIsOverWithUIView"); [activityIndicator stopAnimating]; [self.delegate CustomeAlertViewDismiss:self]; } @end
// // ViewController.h // AlertDemo // // Created by PSH_Chen_Tao on 7/19/13. // Copyright (c) 2013 wolfman. All rights reserved. // #import <UIKit/UIKit.h> #import "CustomeAlertView.h" @interface ViewController : UIViewController <UIAlertViewDelegate,CustomeAlertViewDelegate> - (IBAction)showTraditionAlert:(id)sender; - (IBAction)showWindowAlert:(id)sender; @property(strong,nonatomic) UIActivityIndicatorView *indicator; @property(strong,nonatomic)CustomeAlertView *customeAlertView; @end

// // ViewController.m // AlertDemo // // Created by PSH_Chen_Tao on 7/19/13. // Copyright (c) 2013 wolfman. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize indicator; @synthesize customeAlertView; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)showTraditionAlert:(id)sender { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"正在下載....." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; alertView.delegate = self; [alertView show]; } - (IBAction)showWindowAlert:(id)sender { customeAlertView = [[CustomeAlertView alloc]init]; customeAlertView.delegate = self; [customeAlertView show]; } #pragma mark -- UIAlertViewDelegate //實現代理增加網絡指示器 - (void)willPresentAlertView:(UIAlertView *)alertView;{ indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; indicator.frame = CGRectMake(110, 20, 50, 50); [alertView addSubview:indicator]; [indicator startAnimating]; [indicator release]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ [indicator stopAnimating]; } #pragma mark -- CustomeAlertViewDelegate -(void)CustomeAlertViewDismiss:(CustomeAlertView *) alertView{ [alertView release]; NSLog(@"CustomeAlertViewDismiss"); } @end
對於UIWindow的相關東西可以參考 http://www.cnblogs.com/smileEvday/archive/2012/03/27/2420362.html#2728097
注意設計到UIWindow顯示的工程不能用arc ,具體原因還沒找到讓我信服的 ,希望知道的大牛可以回復下。。。。。。。