工作需要,要做一个类似于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
效果如下:
源码下载:http://download.csdn.net/detail/shencaifeixia1/4485964
如果不清楚,可以相互交流
分享到:
相关推荐
在项目里面点击按钮或者Cell后,显示通知
iOS-授之以鱼不如授之以渔=_=带你升入理解自定义转场动画 详细解析:http://blog.csdn.net/u014220518/article/details/73854424
底部弹出alertsheet,简单易用 NiceAlertSheet *alertSheet = [[NiceAlertSheet alloc] initWithMessage:@"选择性别" choiceButtonTitles:@[@"男", @"女"]]; [alertSheet show]; alertSheet....
自定义Alert,自适应高度
自定义Operation队列简单实现直播礼物动画
IOS 使用XIB 自定义View http://blog.csdn.net/li6185377/article/details/8131042
自定义CollectionView轮播图,不依赖任何图片加载框架,可实现自定义pageControl,自定义轮播View,自定义滚动样式等等 github地址: https://github.com/gltwy/LTAutoScrollView
ios scrollview循环 支持自定义点击事件,简单易于添加到自己的项目中!ARC
自定义下拉刷新动画
ios地图自定义标注和气泡
自定义UIPageControl,实现同时显示不同大小的圆点
自定义双向slider双向滑动实现文字区间选择并显示
该demo实现了iOS客户单自定义表情相关功能。 自定义表情选择器——实现了类似于聊天时自定义表情的显示及选择功能。 聊天时选择表情后输入框的显示——实现了类似于微信聊天时选择表情后输入框的文字显示。 聊天消息...
自定义转场动画
自定义SwitchButton实现IOS中的UISwitch动画效果
自定义模态视图的非交互式转场动画,提供了几种常见的动画形式,并支持自定义动画的拓展; Demo的GitHub地址:https://github.com/fanxiaocong/XCPresentation 喜欢的朋友请给个Star,谢谢!
iOS 开发 自定义二维码扫描
其中,ios6alert.js是插件主要部分的js,ios6alert.css中定义的是对话框的默认样式,可以根据需要进行编辑。 对话框的HTML结构是通过js动态添加到页面上的,所以不需要在页面上额外准备内容。 只需要运行$("body")....
自定义 绘制文字动画 加载动画