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

本文档版权归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.解释代码如下:

 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值