使用UIView类提供的功能来显示动画的例子

来源:互联网 发布:websocket for python 编辑:程序博客网 时间:2024/06/01 10:11

本文档版权归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

{

    [myIVrelease];

    [myButtonrelease];

    [superdealloc];

}


- (void)didReceiveMemoryWarning

{

   // Releases the view if it doesn't have a superview.

    [superdidReceiveMemoryWarning];

    

   // 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

{

    [myIVrelease];

    myIV = nil;

    [myButtonrelease];

    myButton = nil;

    [superviewDidUnload];

   // 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;

    


[UIViewbeginAnimations:@"aa"context:nil];

[UIViewsetAnimationCurve:UIViewAnimationCurveEaseIn];

[UIViewsetAnimationDuration:2.0];

[myIVsetFrame:frame];

    [UIViewsetAnimationDelegate:self];

[UIViewsetAnimationDidStopSelector:@selector(viewExchangeAnimationDidStop:finished:context:)];

[UIViewcommitAnimations];

    

}


- (void) viewExchangeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

   

CGRect frame =myIV.frame;

    frame.origin.y =25;

[UIViewbeginAnimations:@"aa"context:nil];

[UIViewsetAnimationCurve:UIViewAnimationCurveEaseOut];

[UIViewsetAnimationDuration:2.0];

[myIVsetFrame:frame];

[UIViewcommitAnimations];

}

@end


6.解释代码如下:

 CGRect frame = myIV.frame;

    frame.origin.y = 300;

    


[UIView beginAnimations:@"aa" context:nil];//开始一个动画

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];//设置动画先慢后快

[UIView setAnimationDuration:2.0];//设置动画持续2秒

[myIV setFrame:frame];//动画内容,从现有位置移动到frame指示的位置。

    [UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:@selector(viewExchangeAnimationDidStop:finished:context:)];

//上面的两句话设置,当动画结束的时候,调用本类的viewExchangeAnimationDidStop:finished:context:函数

[UIView commitAnimations];//开始动画


其他的代码就不解释了,

7.例子代码

http://download.csdn.net/detail/NickTang/3690975