iOS开发之动画编程的几种方法

IOS中的动画总结来说有五种:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKeyframeAnimation>,NSTimer

这里我就总结了一下这五种方法,其实iOS开发中动画的编程都会在这里面变化,所以只要弄懂了这些动画编程就不难了。

一:UIView动画

一般方式

  1. [UIView beginAnimations:@"ddd" context:nil];//设置动画
  2. [UIView commitAnimations]; //提交动画
  3. 这两个是必须有的,然后在两句的中间添加动画的代码
  4. [UIView beginAnimations:@"ddd" context:nil];//设置动画 ddd为动画名称
  5. [UIView setAnimationDuration:3];//定义动画持续时间
  6. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //setAnimationCurve来定义动画加速或减速方式
  7. [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.window cache:YES];
  8. //设置动画的样式 forView为哪个view实现这个动画效果
  9. [UIView setAnimationDelay:3]; //设置动画延迟多久执行
  10. [UIView setAnimationDelegate:self]; //设置动画的代理 实现动画执行前后的方法 在commitAnimation之前设置
  11. [UIView setAnimationDidStopSelector:@selector(stop)];//设置动画结束后执行的方法
  12. [UIView setAnimationWillStartSelector:@selector(star)];//设置动画将要开始执行的方法
  13. [UIView commitAnimations]; //提交动画
  • typedef enum {
  • UIViewAnimationTransitionNone, //普通状态
  • UIViewAnimationTransitionFlipFromLeft, //从左往右翻转
  • UIViewAnimationTransitionFlipFromRight, //从右往左翻转
  • UIViewAnimationTransitionCurlUp, //向上翻页
  • UIViewAnimationTransitionCurlDown, //向下翻页
  • } UIViewAnimationTransition;
  • typedef enum {
  • UIViewAnimationCurveEaseInOut,
  • UIViewAnimationCurveEaseIn,
  • UIViewAnimationCurveEaseOut,
  • UIViewAnimationCurveLinear
  • } UIViewAnimationCurve;
  1. [UIView beginAnimations:@"ddd" context:nil]; //设置动画
  2. view.frame = CGRectMake(200, 200, 100, 100);
  3. [UIView commitAnimations]; //提交动画
  4. 当view从本来的frame移动到新的frame时会慢慢渐变 而不是一下就完成了 中间也可以添加到上面那段中间 只是多种效果重叠
  5. 以下这些也可以加到 [UIView beginAnimations:@"ddd" context:nil]; [UIView commitAnimations];之间
  6. view.transform = CGAffineTransformMakeTranslation(10, 10);//设置偏移量 相对于最初的 只能偏移一次
  7. view.transform = CGAffineTransformTranslate(view.transform, 10, 10); //设置偏移量 偏移多次
  8. self.view.transform = CGAffineTransformMakeRotation(M_PI);//设置旋转度 只能旋转一次
  9. self.view.transform = CGAffineTransformRotate(self.view.transform, M_PI); //旋转多次
  10. self.view.transform = CGAffineTransformMakeScale(1.1, 1.1); //设置大小 只能改变一次 数值时相对于本来的几倍
  11. self.view.transform = CGAffineTransformScale(self.view.transform, 1.1, 1.1);//改变多次
  12. self.view.transform = CGAffineTransformIdentity;//回到当初的样子 执行一次
  13. self.view.transform = CGAffineTransformInvert(self.view.transform);//得到相反的样子 大小 方向 位置执行多次

这里我实现了一个自定义的动画方法,方便使用,只需要调用就可以实现很好的功能。

方法的实现

-(void)UIViewAnimation:(UIView* )view frame:(CGRect)frame type:(int)type alpha:(float)alpha duration:(float)duration

{

//将对应的参数实现在方法中,调用的时候只需要输入方法中所需要的参数就能很好的调用这个方法,并且实现想要的功能!

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:duration];

[UIView setAnimationCurve:type];

[UIView setAnimationDelegate:self];

view.alpha=alpha;

view.frame=frame;

[UIView commitAnimations];

}

调用方法

[self UIViewAnimation:downView frame:CGRectMake(0, height, 320, 58) type:UIViewAnimationCurveEaseOut alpha:1 duration:0.3];

Block方式

  1. [UIView animateWithDuration:3 animations:^(void){
  2. //这里相当于在begin和commint之间
  3. }completion:^(BOOL finished){
  4. //这里相当于动画执行完成后要执行的方法,可以继续嵌套block
  5. }];

高级一点的block动画(Next)内嵌

- (void)changeUIView{  
    [UIView animateWithDuration:2  delay:0   options:UIViewAnimationOptionCurveEaseOut animations:^(void){  
        moveView.alpha = 0.0;  
      }completion:^(BOOL finished){  
              [UIView animateWithDuration:1 delay:1.0   options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat    animations:^(void){                [UIView setAnimationRepeatCount:2.5];  
                  moveView.alpha = 1.0;  
              }completion:^(BOOL finished){                   
           }];       
    }];  
}  

二:.CAAnimation

需要添加库,和包含头文件

caanimation有多个子类

CABasicAnimation

  1. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  2. //@""里的字符串有多种,可以自己找相关资料,一定要填对,动画才会执行 opacity设置透明度 bounds.size设置大小
  3. [animation setFromValue:[NSNumber numberWithFloat:1.0]]; //设置透明度从几开始
  4. [animation setToValue:[NSNumber numberWithFloat:0.3]];//设置透明度到几结束
  5. [animation setDuration:0.1]; //设置动画时间
  6. [animation setRepeatCount:100000];//设置重复时间
  7. [animation setRepeatDuration:4]; //会限制重复次数
  8. [animation setAutoreverses:NO];//设置是否从1.0到0.3 再从0.3到1.0 为一次 如果设置为NO则 1.0到0.3为一次
  9. [animation setRemovedOnCompletion:YES]; //完成时移出动画 默认也是
  10. [view.layer addAnimation:animation forKey:@"abc"];//执行动画

CAKeyframeAnimation

  1. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];//设置view从初始位置经过一系列点
  2. NSArray *postionAraay = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(100, 20)], [NSValue valueWithCGPoint:CGPointMake(40, 80)],[NSValue
  3. valueWithCGPoint:CGPointMake(30, 60)],[NSValue valueWithCGPoint:CGPointMake(20, 40)],[NSValue valueWithCGPoint:CGPointMake(0, 100)],nil];//设置点
  4. NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.3],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.6],[NSNumber numberWithFloat:0.1],[NSNumber
  5. numberWithFloat:1.0], nil]; //设置移动过程的时间
  6. [animation setKeyTimes:times];
  7. [animation setValues:postionAraay];
  8. [animation setDuration:5]; //设置动画时间
  9. [bigImage.layer addAnimation:animation forKey:@"dd"]; //执行动画

CATransition

  1. CATransition *animation = [CATransition animation];
  2. animation.duration = 0.5f;
  3. animation.timingFunction = UIViewAnimationCurveEaseInOut;
  4. animation.fillMode = kCAFillModeForwards;
  • /*
  • kCATransitionFade;
  • kCATransitionMoveIn;
  • kCATransitionPush;
  • kCATransitionReveal;
  • */
  • /*
  • kCATransitionFromRight;
  • kCATransitionFromLeft;
  • kCATransitionFromTop;
  • kCATransitionFromBottom;
  • */
  1. animation.type = kCATransitionPush;
  2. animation.subtype = kCATransitionFromBottom;
  3. [view.layer addAnimation:animation forKey:animation];
  4. type也可以直接用字符串
  • /*
  • cube
  • suckEffect 卷走
  • oglFlip 翻转
  • rippleEffect 水波
  • pageCurl 翻页
  • pageUnCurl
  • cameraIrisHollowOpen
  • cameraIrisHollowClose
  • */

三:NSTimer

这是一种定时器来操作动画的方法,他可以结合上面的方法来实现动画的多样化!

  1. -(void) onTimer {
  2. imageView.center = CGPointMake(imageView.center.x + delta.x,
  3. imageView.center.y + delta.y);
  4. if (imageView.center.x > self.view.bounds.size.width - ballRadius ||
  5. imageView.center.x < ballRadius)
  6. delta.x = -delta.x;
  7. if (imageView.center.y > self.view.bounds.size.height - ballRadius ||
  8. imageView.center.y < ballRadius)
  9. delta.y = -delta.y;
  10. }
  11. - (void) viewDidLoad {
  12. ballRadius = imageView.bounds.size.width / 2;
  13. [slider setShowValue:YES];
  14. delta = CGPointMake(12.0,4.0);
  15. timer = [NSTimer scheduledTimerWithTimeInterval:slider.value
  16. target:self
  17. selector:@selector(onTimer)
  18. userInfo:nil
  19. repeats:YES];
  20. [super viewDidLoad];
  21. }
  22. -(IBAction) sliderMoved:(id) sender {
  23. [timer invalidate];
  24. timer = [NSTimer scheduledTimerWithTimeInterval:slider.value
  25. target:self
  26. selector:@selector(onTimer)
  27. userInfo:nil
  28. repeats:YES];
  29. //
    1. timer = [NSTimer scheduledTimerWithTimeInterval:?
    2. invocation:?
    3. repeats:YES];
  30. }

iOS开发之动画编程的几种方法的更多相关文章

  1. IOS开发中数据持久化的几种方法--NSUserDefaults

    IOS开发中数据持久化的几种方法--NSUserDefaults IOS 开发中,经常会遇到需要把一些数据保存在本地的情况,那么这个时候我们有以下几种可以选择的方案: 一.使用NSUserDefaul ...

  2. iOS开发进阶-实现多线程的3种方法

    相关文章链接: 1.多线程简介 2.实现多线程的3种方法 ......待续 前言 在多线程简介中,我已经说明过了,为了提高界面的流畅度以及用户体验.我们务必要把耗时的操作放到别的线程中去执行,千万不要 ...

  3. 【转】 ios开发之倒计时实现的两种方法

    原文:http://blog.csdn.net/kylinbl/article/details/8972261 方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTime ...

  4. iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>

    前言:根据前篇<iOS开发之网络编程--2.NSURLSessionDownloadTask文件下载>或者<iOS开发之网络编程--3.NSURLSessionDataTask实现文 ...

  5. iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)

    前言:使用NSURLSessionDownloadTask满足不这个需要离线断点续传的下载需求,所以这里就需要使用NSURLSessionDataTask的代理方法来处理下载大文件,并且实现离线断点续 ...

  6. iOS开发之网络编程--小文件下载

    文件下载方式: 如果下载的文件比较小,下载方式: 直接用NSData的 +(id)dataWithContentsOfURL:(NSURL*)url; 利用NSURLConnection发送一个HTT ...

  7. iOS开发之网络编程--5、NSURLSessionUploadTask+NSURLSessionDataDelegate代理上传

    前言:关于NSURLSession的主要内容快到尾声了,这里就讲讲文件上传.关于文件上传当然就要使用NSURLSessionUploadTask,这里直接讲解常用的会和代理NSURLSessionDa ...

  8. iOS开发之网络编程--2、NSURLSessionDownloadTask文件下载

    本文内容大纲: 1.回顾NSURLSessionTask 2.NSURLSessionDownloadTask大文件之block下载 3.NSURLSessionDownloadTask大文件之代理方 ...

  9. iOS开发之网络编程--使用NSURLConnection实现文件上传

    前言:使用NSURLConnection实现文件上传有点繁琐.    本文并没有介绍使用第三方框架上传文件. 正文: 这里先提供用于编码测试的接口:http://120.25.226.186:3281 ...

随机推荐

  1. empty()方法

    empty()可以用来检查一个变量是否被声明或者值为false,通常被用来检查一个表单变量是否被发送或者包含数据. 例如一个登录表单: <?php if(!empty($_POST['uname ...

  2. SQL viewId 比较好看的 Id

    有时候我们希望 Id 要好看一些,比如 Id=1 -> Id=T000001 refer : http://www.kodyaz.com/t-sql/custom-sequence-string ...

  3. POJ 1987 Distance Statistics

    http://poj.org/problem?id=1987 题意:给一棵树,求树上有多少对节点满足距离<=K 思路:点分治,我们考虑把每个距离都存起来,然后排序,一遍扫描计算一下,注意还要减掉 ...

  4. Gridview中将某列的背景设置为绿色

    状态字段是:archivesStatus,archivesStatus为1时,设置背景色 protected void gvInfo_RowDataBound(object sender, GridV ...

  5. 在QT程序中使用cout和cin

    1先输入10个数字,再输出. #include <QtCore/QCoreApplication> #include <QtCore/QList> #include <Q ...

  6. Smarty 使用继承方式实现配置

    . 常用配置选项 在使用Smarty模板引擎之前,我们必须先学习如何配置Smarty的选项.而在Smarty的常见选项中,我们首先必须了解4个最基本的目录选项. 模板目录(template):本目录用 ...

  7. zabbix 插件使用问题

    [elk@dr-mysql01 frontend]$ ../../bin/logstash -f std02.conf Settings: Default pipeline workers: 8 Pi ...

  8. crontab,at命令,常见问题

    crontab命令 前 一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的. Linux 系统上面原本就有非常 ...

  9. Numerical Methods with MATLAB(1)

    目前正在阅读MATLAB相关的书籍:Numerical Methods with MATLAB,现在感觉这本书写的还行, 细致基础,而且写的比较清楚,同时把malab和数值算法结合在一起. 目前刚看完 ...

  10. 关于解决Oracle登录:ora-12154:tns:无法解析指定的连接标识符

    (注:此文摘自http://www.linuxidc.com/Linux/2012-04/59322.htm) 开始学习Oracle,所以今天就打算把Oracle 10g安装下.安装完后就来进行测试是 ...