ios中自定义alert view,并实现动画组合
来源:互联网 发布:sql 无法删除表 编辑:程序博客网 时间:2024/05/21 15:08
============================================================
博文原创,转载请声明出处
电子咖啡(原id蓝岩)
============================================================
工作需要,要做一个类似于alert的view,在上面可以自己随便画东西,在网上找了一个开源的例子,读了源码之后,感觉对ios的view和动画有了更深的了解,现在与大家分享。下面是其中自定义alertview和动画页面的主要代码,有我的注释,
- //
- // CustomizedAlertViewDemoAppDelegate.m
- // CustomizedAlertViewDemo
- //
- // Created by CocoaBob on 11-3-22.
- // Copyright 2011 CocoaBob. All rights reserved.
- //
- #import "CustomizedAlertViewDemoAppDelegate.h"
- @implementation CustomizedAlertViewDemoAppDelegate
- @synthesize window;
- #pragma mark -
- #pragma mark Application lifecycle
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- //设置显示按钮背景,stretchableImageWithLeftCapWidth:topCapHeight:设置图片的不可拉伸去,
- [mShowAlertButton setBackgroundImage:[[UIImage imageNamed:@"button_white"] stretchableImageWithLeftCapWidth:6.0 topCapHeight:6.0] forState:UIControlStateNormal];
- [mShowAlertButton setBackgroundImage:[[UIImage imageNamed:@"button_white_highlight"] stretchableImageWithLeftCapWidth:6.0 topCapHeight:6.0] forState:UIControlStateHighlighted];
- //设置alert的背景
- UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"alert-view-bg-portrait"] stretchableImageWithLeftCapWidth:142 topCapHeight:31]];
- [backgroundView setFrame:mAlertView.bounds];
- //将alert插入到背景中
- [mAlertView insertSubview:backgroundView atIndex:0];
- [self.window makeKeyAndVisible];
- return YES;
- }
- - (void)dealloc {
- [window release];
- [super dealloc];
- }
- #pragma mark animations
- static CGFloat kTransitionDuration = 2;
- - (CGAffineTransform)transformForOrientation {
- UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
- if (orientation == UIInterfaceOrientationLandscapeLeft) {
- return CGAffineTransformMakeRotation(M_PI*1.5);
- } else if (orientation == UIInterfaceOrientationLandscapeRight) {
- return CGAffineTransformMakeRotation(M_PI/2);
- } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
- return CGAffineTransformMakeRotation(-M_PI);
- } else {
- return CGAffineTransformIdentity;
- }
- }
- - (void)bounce2AnimationStopped{
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:kTransitionDuration/2];
- mAlertView.transform = [self transformForOrientation];
- [UIView commitAnimations];
- }
- - (void)bounce1AnimationStopped{
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:kTransitionDuration/2];
- [UIView setAnimationDelegate:self];
- [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)];
- mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 1.5, 1.5);
- [UIView commitAnimations];
- }
- - (void)alertViewIsRemoved{
- [[mAlertViewSuperView retain] removeFromSuperview];
- [mTempFullscreenWindow release];
- mTempFullscreenWindow = nil;
- }
- #pragma mark IBAction
- - (IBAction)showAlertView:(id)sender{
- //mTempFullscreenWindow对应的是alert外围的不可交互window
- if (!mTempFullscreenWindow) {
- //设置为屏幕大小
- mTempFullscreenWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- //这里是设置window的层次等级,参见UIWindowLevel
- mTempFullscreenWindow.windowLevel = UIWindowLevelStatusBar;
- //设置背景颜色,即灰色透明
- mTempFullscreenWindow.backgroundColor = [UIColor clearColor];
- //将AlertViewSuperView加入window
- [mTempFullscreenWindow addSubview:mAlertViewSuperView];
- [mAlertViewSuperView setAlpha:0.0f];
- [mAlertViewSuperView setFrame:[mTempFullscreenWindow bounds]];
- [mTempFullscreenWindow makeKeyAndVisible];
- //创建旋转动画
- CABasicAnimation* rotationAnimation;
- rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
- rotationAnimation.duration = 0.5;
- rotationAnimation.cumulative = YES;
- rotationAnimation.repeatCount = 1;
- //创建转移缩放动画
- mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 0.1, 0.1);
- //开始动画,调用此函数之后,可以就绪修改animation的动画参数,可以让动画有渐变的效果,
- //beginAnimations:context:可以多次使用,来嵌套动画的显示
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:1];
- [UIView setAnimationDelegate:self];
- //设置动画停止监听函数
- [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)];
- // mAlertView.transform 是衔接上面的动画,进行动画嵌套
- mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 0.5, 1.5);
- [mAlertViewSuperView setAlpha:1.0f];
- //嵌套重新使用
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:1];
- [UIView setAnimationDelegate:self];
- mAlertView.transform = CGAffineTransformScale([self transformForOrientation], 1, 1);
- [UIView commitAnimations];
- //开始旋转动画
- [mAlertView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
- }
- }
- - (IBAction)dismissAlertView:(id)sender{
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:kTransitionDuration/2];
- [UIView setAnimationDelegate:self];
- [UIView setAnimationDidStopSelector:@selector(alertViewIsRemoved)];
- [mAlertViewSuperView setAlpha:0.0f];
- [UIView commitAnimations];
- }
- @end
效果如下:
如果不清楚,可以相互交流
- ios中自定义alert view,并实现动画组合
- ios中自定义alert view,并实现动画组合
- iOS动画篇:自定义View
- Android:自定义view实现动画
- 自定义view+属性动画实现
- 自定义view中绘制动画
- ios中自定义view
- Android中自定义View和自定义动画
- Android中自定义View和自定义动画
- 一步一步实现Android自定义组合View
- 自定义view继承viewgroup,实现组合按钮。
- iOS 自定义雷达 扫描/扩散动画 View
- ios 组合动画实现图片浏览,效果为图片放大并淡出
- iOS中自定义View实现layoutSubviews布局子控件
- iOS中自定义View实现layoutSubviews布局子控件
- Android自定义View实现雷达扫描动画
- 自定义View——实现波浪动画
- Android自定义View实现雷达扫描动画
- java List 排序 Collections.sort() 对 List 排序
- _OBJC_CLASS_$_SKStoreProductViewController
- javascript技巧
- 封装PageModel分页实现查询功能
- vc6.0 编译完成端口程序出错
- ios中自定义alert view,并实现动画组合
- Android中的WiFi开发简明教程
- 使用chrome调试手机,平板,pc上各种浏览器上的页面
- WizFamilyDay
- C字符存诸操作
- Netflix推荐系统:从评分预测到消费者法则
- 一步步构建大型网站架构
- jdbc简单模板技术
- C冒泡排序 @100到200素数