本文档版权归NickTang所有,没有本人书面或电子邮件允许,不许转载,摘录,发表。多谢!
上一个文档,我演示了timer的使用,并且形成了一个动画,但是这个动画可扩展性不好,我们需要更好的动画实现技术,这里UIView类提供了一些基本的功能。
1.新建一个view-based Application.(在iOS5中是Single View Application)
2.加入一个小的图片,我用的是一个circle.png,长和宽都不要大于100.
3.在viewcontroller.xib上面做如下布局
4. ViewController.h文件如下:
@interface subViewAnimationViewController : UIViewController {
IBOutlet UIButton *myButton;
IBOutlet UIImageView *myIV;
}
- (IBAction)startAnimation:(id)sender;
@end
5. ViewController.m文件如下:
@implementation subViewAnimationViewController
- (void)dealloc
{
[myIV release];
[myButton release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[myIV release];
myIV = nil;
[myButton release];
myButton = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)startAnimation:(id)sender {
CGRect frame = myIV.frame;
frame.origin.y = 300;
[UIView beginAnimations:@"aa" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2.0];
[myIV setFrame:frame];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(viewExchangeAnimationDidStop:finished:context:)];
[UIView commitAnimations];
}
- (void) viewExchangeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
CGRect frame = myIV.frame;
frame.origin.y = 25;
[UIView beginAnimations:@"aa" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:2.0];
[myIV setFrame:frame];
[UIView commitAnimations];
}
@end
6.解释代码如下:
CGRectframe =myIV.frame;
frame.origin.y=300;
[UIViewbeginAnimations:@"aa"context:nil];//开始一个动画
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseIn];//设置动画先慢后快
[UIViewsetAnimationDuration:2.0];//设置动画持续2秒
[myIVsetFrame:frame];//动画内容,从现有位置移动到frame指示的位置。
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationDidStopSelector:@selector(viewExchangeAnimationDidStop:finished:context:)];
//上面的两句话设置,当动画结束的时候,调用本类的viewExchangeAnimationDidStop:finished:context:函数
[UIViewcommitAnimations];//开始动画
其他的代码就不解释了,
7.例子代码