ios中自定义alert view,并实现动画组合

来源:互联网 发布:sql 无法删除表 编辑:程序博客网 时间:2024/05/21 15:08
============================================================
博文原创,转载请声明出处
电子咖啡(原id蓝岩)
============================================================
工作需要,要做一个类似于alert的view,在上面可以自己随便画东西,在网上找了一个开源的例子,读了源码之后,感觉对ios的view和动画有了更深的了解,现在与大家分享。

下面是其中自定义alertview和动画页面的主要代码,有我的注释,

[html] view plaincopyprint?
  1. //  
  2. //  CustomizedAlertViewDemoAppDelegate.m  
  3. //  CustomizedAlertViewDemo  
  4. //  
  5. //  Created by CocoaBob on 11-3-22.  
  6. //  Copyright 2011 CocoaBob. All rights reserved.  
  7. //  
  8.   
  9. #import "CustomizedAlertViewDemoAppDelegate.h"  
  10.   
  11. @implementation CustomizedAlertViewDemoAppDelegate  
  12.   
  13. @synthesize window;  
  14.   
  15. #pragma mark -  
  16. #pragma mark Application lifecycle  
  17.   
  18. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  19.       
  20.     //设置显示按钮背景,stretchableImageWithLeftCapWidth:topCapHeight:设置图片的不可拉伸去,  
  21.     [mShowAlertButton setBackgroundImage:[[UIImage imageNamed:@"button_white"] stretchableImageWithLeftCapWidth:6.0 topCapHeight:6.0] forState:UIControlStateNormal];  
  22.     [mShowAlertButton setBackgroundImage:[[UIImage imageNamed:@"button_white_highlight"] stretchableImageWithLeftCapWidth:6.0 topCapHeight:6.0] forState:UIControlStateHighlighted];  
  23.       
  24.     //设置alert的背景  
  25.     UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"alert-view-bg-portrait"] stretchableImageWithLeftCapWidth:142 topCapHeight:31]];  
  26.     [backgroundView setFrame:mAlertView.bounds];  
  27.       
  28.     //将alert插入到背景中  
  29.     [mAlertView insertSubview:backgroundView atIndex:0];  
  30.       
  31.     [self.window makeKeyAndVisible];  
  32.     return YES;  
  33. }  
  34.   
  35. - (void)dealloc {  
  36.     [window release];  
  37.     [super dealloc];  
  38. }  
  39.   
  40. #pragma mark animations  
  41.   
  42. static CGFloat kTransitionDuration = 2;  
  43.   
  44. - (CGAffineTransform)transformForOrientation {  
  45.     UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
  46.     if (orientation == UIInterfaceOrientationLandscapeLeft) {  
  47.         return CGAffineTransformMakeRotation(M_PI*1.5);  
  48.     } else if (orientation == UIInterfaceOrientationLandscapeRight) {  
  49.         return CGAffineTransformMakeRotation(M_PI/2);  
  50.     } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {  
  51.         return CGAffineTransformMakeRotation(-M_PI);  
  52.     } else {  
  53.         return CGAffineTransformIdentity;  
  54.     }  
  55. }  
  56.   
  57. - (void)bounce2AnimationStopped{  
  58.     [UIView beginAnimations:nil context:nil];  
  59.     [UIView setAnimationDuration:kTransitionDuration/2];  
  60.     mAlertView.transform = [self transformForOrientation];  
  61.     [UIView commitAnimations];  
  62. }  
  63.   
  64. - (void)bounce1AnimationStopped{  
  65.     [UIView beginAnimations:nil context:nil];  
  66.     [UIView setAnimationDuration:kTransitionDuration/2];  
  67.     [UIView setAnimationDelegate:self];  
  68.     [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)];  
  69.     mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 1.5, 1.5);  
  70.     [UIView commitAnimations];  
  71. }  
  72.   
  73. - (void)alertViewIsRemoved{  
  74.     [[mAlertViewSuperView retain] removeFromSuperview];  
  75.     [mTempFullscreenWindow release];  
  76.     mTempFullscreenWindow = nil;  
  77. }  
  78.   
  79. #pragma mark IBAction  
  80.   
  81. - (IBAction)showAlertView:(id)sender{  
  82.     //mTempFullscreenWindow对应的是alert外围的不可交互window  
  83.     if (!mTempFullscreenWindow) {  
  84.         //设置为屏幕大小  
  85.         mTempFullscreenWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  86.         //这里是设置window的层次等级,参见UIWindowLevel  
  87.         mTempFullscreenWindow.windowLevel = UIWindowLevelStatusBar;  
  88.         //设置背景颜色,即灰色透明  
  89.         mTempFullscreenWindow.backgroundColor = [UIColor clearColor];  
  90.         //将AlertViewSuperView加入window  
  91.         [mTempFullscreenWindow addSubview:mAlertViewSuperView];  
  92.         [mAlertViewSuperView setAlpha:0.0f];  
  93.         [mAlertViewSuperView setFrame:[mTempFullscreenWindow bounds]];  
  94.         [mTempFullscreenWindow makeKeyAndVisible];  
  95.           
  96.         //创建旋转动画  
  97.         CABasicAnimation* rotationAnimation;  
  98.         rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  
  99.         rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0  ];  
  100.         rotationAnimation.duration = 0.5;  
  101.         rotationAnimation.cumulative = YES;  
  102.         rotationAnimation.repeatCount = 1;  
  103.           
  104.           
  105.           
  106.         //创建转移缩放动画  
  107.         mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 0.1, 0.1);   
  108.         //开始动画,调用此函数之后,可以就绪修改animation的动画参数,可以让动画有渐变的效果,  
  109.         //beginAnimations:context:可以多次使用,来嵌套动画的显示  
  110.         [UIView beginAnimations:nil context:nil];          
  111.         [UIView setAnimationDuration:1];  
  112.         [UIView setAnimationDelegate:self];  
  113.         //设置动画停止监听函数  
  114.         [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)];  
  115.         // mAlertView.transform 是衔接上面的动画,进行动画嵌套  
  116.         mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 0.5, 1.5);  
  117.         [mAlertViewSuperView setAlpha:1.0f];  
  118.           
  119.         //嵌套重新使用  
  120.         [UIView beginAnimations:nil context:nil];          
  121.         [UIView setAnimationDuration:1];  
  122.         [UIView setAnimationDelegate:self];  
  123.         mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 1, 1);  
  124.           
  125.         [UIView commitAnimations];  
  126.         //开始旋转动画  
  127.         [mAlertView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];  
  128.     }  
  129. }  
  130.   
  131. - (IBAction)dismissAlertView:(id)sender{  
  132.     [UIView beginAnimations:nil context:nil];  
  133.     [UIView setAnimationDuration:kTransitionDuration/2];  
  134.     [UIView setAnimationDelegate:self];  
  135.     [UIView setAnimationDidStopSelector:@selector(alertViewIsRemoved)];  
  136.     [mAlertViewSuperView setAlpha:0.0f];  
  137.     [UIView commitAnimations];  
  138. }  
  139.   
  140.   
  141. @end  

效果如下:


源码下载:http://download.csdn.net/detail/shencaifeixia1/4485964

如果不清楚,可以相互交流

原创粉丝点击