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

一: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:[NSValuevalueWithCGPoint: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开发之动画编程的几种方法

    iOS开发之动画编程的几种方法 IOS中的动画总结来说有五种:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKe ...

  2. iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程

    iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程 一.引言 二.初识CoreAnimation 三.锚点对几何属性的影响 四.Layer与View之间的关系 ...

  3. 转一篇简洁的UIView动画编程方法

    iOS  中的 UIView 动画编程其实还是很简单的,像 CSS3 一样,在给定的时间内完成状态连续性的变化呈现.比如背景色,Frame 大小,位移.翻转,特明度等. 以前我使用的编程方式都是用下面 ...

  4. Swift动画编程指南-01 简介

    大家好,我是老镇,这段时间家里和工作上发生了很多的事情,所以很长一段时间都没有出来搞什么小动作了.在接下来的一段时间内我会制作一些列关于使用Swift进行动画编程的视频,希望和大家胃口. 在iOS的世 ...

  5. iOS 6编程Cookbook(影印版)

    <iOS 6编程Cookbook(影印版)> 基本信息 原书名:iOS 6 Programming Cookbook 作者: Vandad Nahavandipoor 出版社:东南大学出版 ...

  6. ios 动画效果CATransition笔记

    初学ios开发,很多概念还不清楚,所以只有边学边做例子.又怕学了后面忘了前面,因此用自己的博客来纪录自己的学习历程,也是对自己学习不要懈怠做个监督. 刚学ios做动画效果.因为ios封装得很好,实现i ...

  7. IOS动画总结

    IOS动画总结   一.基本方式:使用UIView类的UIViewAnimation扩展 + (void)beginAnimations:(NSString *)animationID context ...

  8. iOS网络编程模型

    iOS网络编程层次结构也分为三层: Cocoa层:NSURL,Bonjour,Game Kit,WebKit Core Foundation层:基于 C 的 CFNetwork 和 CFNetServ ...

  9. (转)iOS动画Core Animation

    文章转载:http://blog.sina.com.cn/s/blog_7b9d64af0101b8nh.html 在iOS中动画实现技术主要是:Core Animation. Core Animat ...

随机推荐

  1. CodeForces 669C Little Artem and Matrix GNU

    模拟. 把操作记录一下,倒着复原回去. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cs ...

  2. CodeForces 710A King Moves

    简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...

  3. C#基础--值类型和引用类型

    C#中大多数类型都是引用类型,只有个别特殊情况是值类型. 值类型: 枚举(enum) 结构(struct) 基础类型:int, short, char, bool....(string是引用类型) 引 ...

  4. 嵌入式系统基础知识(一): 系统结构和嵌入式Linux

    目录 一. 嵌入式体系结构 二. 开发过程中的分工 三. 嵌入式软件体系结构 四. 嵌入式Linux 一. 嵌入式体系结构 <嵌入式系统设计师教程>这本书的前三章脉络很清晰, 按照嵌入式系 ...

  5. LeetCode 453. Minimum Moves to Equal Array Elements C#

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  6. svn中“clean up”死循环问题解决办法

    SVN在使用update命令时,提示使用"clean up "命令,在使用clean up命令时报错"Previous operation has not finishe ...

  7. stm32 串口乱码的解决

    版权声明:本文为博主原创文章. 前几天在中移物联网申请了一个迷你开发板,运行官方提供的程序,感觉板子是正常的.但是自己写的程序能够刷到板子上,但是串口却是乱码.官方和我的额程序都是用的库函数的方式写的 ...

  8. Github朝花夕拾

    删除fork的项目   下载指定revision的repository 通过git log查看提交历史,最好是GUI查看 然后执行命令git reset –hard <sha1> 同步到最 ...

  9. 将图片设置给ImageView时的属性配置

    将图片设置给ImageView的时候,由于图片大小和逻辑需求的不确定会造成实际产生的效果和我们实际的需求不符的情况,这时需要对imageVIew控件添加scaleType属性,下面我用两张图片帮大家轻 ...

  10. 利用LinkedList实现洗牌功能

    分2步: 1.生成扑克牌. 2.洗牌. package com.dongbin.collection; import java.util.LinkedList; import java.util.Ra ...