•在iOS开发中,想实现一些小动画是非常容易的
- 系统会根据某个属性值的改变自动形成动画
- 比如x值本来是10,然后x值突然改为了100,系统会通过平移动画的方式让x值慢慢从10变到100
•简易动画大致有2种方式:
1. 头尾式
[UIView beginAnimations:nilcontext:nil];
/** 需要执行动画的代码 **/
[UIView commitAnimations];
示例程序如下:
<span style="color:#3333ff;"> </span> //开始动画
[UIView beginAnimations:nil context:nil];
//设置动画时长
[UIView setAnimationDuration:1.0f];
/*
UIViewAnimationCurveEaseInOut, 开始和结束慢速slow at beginning and end
UIViewAnimationCurveEaseIn, 开始慢速slow at beginning
UIViewAnimationCurveEaseOut, 结束时慢速slow at end
UIViewAnimationCurveLinear 匀速
*/
//设置动画模式
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//设置重复次数default = 0.0 最大值:MAXFLOAT HUGE_VAL
[UIView setAnimationRepeatCount:1];
//设置动画的代理
[UIView setAnimationDelegate:self];
设置动画将要开始时的代理方法
[UIView setAnimationWillStartSelector:@selector(animationWillStart)];
//设置动画结束时的代理方法
[UIView setAnimationDidStopSelector:@selector(moveToButtom)];
//执行动画的代码
CGRect frame = self.view.frame;
frame.origin.x = self.window.bounds.size.width - 100;
[self beginAnimationWithStopSelector:@selector(moveToButtom) frame:frame];
//提交动画
//[UIView commitAnimations];
2. Block式
[UIView animateWithDuration:0.5animations:^{
/**需要执行动画的代码 **/
}];
/**参数:
Duration:动画持续时长
*/
[UIView animateWithDuration:2.0f animations:^{
/**需要执行动画的代码 **/
}completion:^(BOOL finished) {
/**动画结束执行的代码 **/
}];