============================================================
博文原创,转载请声明出处
电子咖啡(原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
效果如下:
如果不清楚,可以相互交流
ref:https://github.com/CocoaBob/CustomizedAlertViewDemo