在iOS中,动画实现方向有两种,一种是操作UIView的animation方法,另外一种就是核心动画,但到iOS7中,UIView又跟核心动画牵扯在一起。

方式一(利用核心动画添加动画)

  • 核心动画的层次关系

  • 转场动画(CATransition)

    • 用于做场景的转换动画,能偶为层提供移出屏幕和一如屏幕的动画效果。
    • UINavigationController就是通过CATransition实现了讲控制器的师徒推入屏幕的动画效果。
    • 常用属性
      • type动画过度类型
      • subtype:动画过度方向
      • startProgress:动画起点(在整体的百分比)(可用的值从0到1,在动画中起点或终点的逗留时间,开始的时间一定要比结束的时间小,下同)
      • endProgress:动画终点(在整体动画的百分比)
  1. CATransition *animation = [CATransition animation];
  2. animation.type = @"reveal";
  3. animation.duration = ;
  4. animation.subtype = kCATransitionReveal;
  5. [self.myView.layer addAnimation:animation forKey:nil];
  6. CGPoint point = self.myView.center;
  7. point.y += ;
  8. [self.myView setCenter:point];
  • 基本动画(CABasicAnimation),是CAPropertyAnimation的子类,一个动画可控制一个属性的变化,变化值只能是两个值中变化,可以在fromValue和toValue两个值中设置

    1. CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    2. baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(, , , )];
    3. baseAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(, , , )];
    4.  
    5. baseAnimation.duration = 2.0;
    6. baseAnimation.removedOnCompletion = NO;
    7. baseAnimation.fillMode = kCAFillModeForwards;
    8. baseAnimation.repeatCount = MAXFLOAT;
    9.  
    10. [self.myView.layer addAnimation:baseAnimation forKey:nil];
  • 帧动画(CAKeyframeAnimation),帧动画也是CAPropertyAnimation的子类,所以也是控制一个view的属性做动画,与CABaseAnimation不同的是,CAKeyFrameAnimation可以添加多个关键帧,而CABaseAnimation可以看做是两个关键帧的帧动画,我们可以好好利用帧动画的关键帧来做比较有趣的动画,如泡泡效果。

  1. CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
  2. // 位移
  3. CAKeyframeAnimation *positionAnima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  4. positionAnima.calculationMode = kCAAnimationCubicPaced;
  5. positionAnima.duration = 5;
  6. positionAnima.fillMode = kCAFillModeForwards;
  7. positionAnima.removedOnCompletion = NO;
  8. positionAnima.repeatCount = MAXFLOAT;
  9. positionAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  10.  
  11. // 添加移动路径
  12. CGMutablePathRef curvedPath = CGPathCreateMutable();
  13. CGRect circleContainer = CGRectInset(myView.frame, myView.frame.size.width / - , myView.frame.size.height / - );
  14. CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
  15. positionAnima.path = curvedPath;
  16. CGPathRelease(curvedPath);
  17. [myView.layer addAnimation:positionAnima forKey:nil];
  18.  
  19. // 缩放X
  20. CAKeyframeAnimation *scaleX = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
  21. scaleX.duration = 1.0;
  22. scaleX.values = @[@1.0,@1.1,@1.0];
  23. scaleX.keyTimes = @[@0.0,@0.5,@1.0];
  24. scaleX.repeatCount = MAXFLOAT;
  25. scaleX.autoreverses = YES;
  26.  
  27. scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  28. [myView.layer addAnimation:scaleX forKey:nil];
  29.  
  30. // 缩放Y
  31. CAKeyframeAnimation *scaleY = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
  32. scaleY.duration = 1.5;
  33. scaleY.values = @[@1.0,@1.1,@1.0];
  34. scaleY.keyTimes = @[@0.0,@0.5,@1.0];
  35. scaleY.repeatCount = MAXFLOAT;
  36. scaleY.autoreverses = YES;
  37.  
  38. group.animations = @[positionAnima,scaleX,scaleY];
  39. scaleY.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  40. [myView.layer addAnimation:scaleY forKey:nil];

运行效果

由于本人的电脑是黑苹果,所以将就一下啦,哈哈,白苹果应该不会这样的。

  • 动画组(CAAnimationGroup)CAAnimation的子类,可以保存一组动画对象,讲CAAnimationGroup对象加入层后,组中所有动画可以同时运行,所以,当我们需要做多个动画并且执行的时间不一样的时候,动画组就不适用。例如上面的泡泡效果。
  1. group.animations = [里面放动画对象];

方式二(利用UIView添加动画)

  • UIView动画(手码)

    • 添加单个动画

      1. [UIView beginAnimations:nil context:nil];
      2. [UIView setAnimationDuration:];
      3. CGPoint point = self.myView.center;
      4. point.y += ;
      5. [self.myView setCenter:point];
      6. [UIView commitAnimations];
    • 添加多个动画
  1.   [UIView beginAnimations:nil context:nil];
  2. [UIView setAnimationDuration:];
  3. CGPoint point = self.myView.center;
  4. point.y += ;
  5. [self.myView setCenter:point];
  6. [UIView commitAnimations];
  7.  
  8. [UIView beginAnimations:nil context:nil];
  9. [UIView setAnimationDuration:];
  10. [self.myView setAlpha:0.1];
  11. [UIView commitAnimations];
  • UIView动画(Block)
  1. [UIView animateWithDuration: animations:^{
  2. CGPoint point = self.myView.center;
  3. point.y += ;
  4. [self.myView setCenter:point];
  5. }];
  • UIView动画(Block帧动画),从iOS7开启,苹果提供了比较便捷的方法来调用帧动画,不用新建帧动画实例,直接对layer的属性进行控制。
  1. [UIView animateKeyframesWithDuration:0.5 delay: options:UIViewKeyframeAnimationOptionAutoreverse animations:^{
  2. self.view.bounds = CGRectMake(, , , );
  3. } completion:^(BOOL finished) {
  4.  
  5. }];
  1.  
  • UIView转场动画。
  1. + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

这个方法应该不好理解,简单来说,这个方法调用完毕后,相当于执行了两句代码,

  1. // 添加toView到父视图
  2. [fromView.superview addSubview:toView];
  3. // 把fromView从父视图中移除
  4. [fromView.superview removeFromSuperview];
  5. - duration:动画的持续时间
  6. - duration:动画的持续时间
  7. - options:转场动画的类型
  8. - animations:将改变视图属性的代码放在这个block
  9. - completion:动画结束后,会自动调用这个block
  1.  

iOS动画实现总结的更多相关文章

  1. (转)iOS动画Core Animation

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

  2. 解析 iOS 动画原理与实现

    这篇文章不会教大家如何实现一个具体的动画效果,我会从动画的本质出发,来说说 iOS 动画的原理与实现方式. 什么是动画 动画,顾名思义,就是能“动”的画.人的眼睛对图像有短暂的记忆效应,所以当眼睛看到 ...

  3. IOS动画隐式,显式,翻页

    //  ViewController.m //  IOS动画0817 // //  Created by 张艳锋 on 15/8/17. //  Copyright (c) 2015年 张艳锋. Al ...

  4. iOS动画原理

    1. iOS动画原理 本质:动画对象(这里是UIView)的状态,基于时间变化的反应 分类:可以分为显式动画(关键帧动画和逐帧动画)和隐式动画 关键帧和逐帧总结:关键帧动画的实现方式,只需要修改某个属 ...

  5. iOS 动画基础

    原文:http://www.cnblogs.com/lujianwenance/p/5733846.html   今天说一下有关动画的基础,希望能帮助到一些刚接触iOS动画或者刚开始学习iOS的同学, ...

  6. ios 动画效果CATransition笔记

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

  7. IOS动画总结

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

  8. ios 动画学习的套路 (二)

    有它们俩你就够了! 说明:下面有些概念我说的不怎么详细,网上实在是太多了,说了我觉得也意义不大了!但链接都给大家了,可以自己去看,重点梳理学习写动画的一个过程和一些好的博客! (一) 说说这两个三方库 ...

  9. iOS动画学习-视觉效果

    CALayer不仅仅是iOS动画学习-CALayer中介绍的那些内容,他还有一些其他属性,比如shadowColor,borderWidth,borderColor等等,这些属性我们只需要简单点设置就 ...

  10. iOS 动画篇 (二) CAShapeLayer与CoreAnimation结合使用

    接上一篇博客 iOS 动画篇(一) Core Animation CAShapeLayer是CALayer的一个子类,使用这个类能够很轻易实现曲线的动画. 先来一个折线动画效果: 示例代码: //1. ...

随机推荐

  1. __init和__exit宏的作用

    原文地址:http://blog.csdn.net/zhenwenxian/article/details/8564574 内核的部分函数带有__init和__exit宏,负责“初始化”和“清理收尾” ...

  2. linux配置防火墙详细步骤(iptables命令使用方法)

    通过本教程操作,请确认您能使用linux本机.如果您使用的是ssh远程,而又不能直接操作本机,那么建议您慎重,慎重,再慎重! 通过iptables我们可以为我们的Linux服务器配置有动态的防火墙,能 ...

  3. Qt:使用自定义的字体

    Qt:使用自定义的字体 1. 下载字体文件 2. 加载字体文件 3. 使用字体   QFontDatabase::addApplicationFont("XENOTRON.TTF" ...

  4. Android tabhost下的activity怎样获取传来的值

    android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...

  5. SQL Server中的分页

    sqlserver2000时的分页思路 .分页查询时,首先将数据排序 select * from MyStudent order by fid desc .取第一页数据 * from MyStuden ...

  6. JDK个目录,以及与环境变量的关系

    最近学习过程中老是看JDK里面的东西,可每次都翻书找,找了又忘.JDK,我们今天来个了断吧........ 一:bin: JDK中所包含的开发工具的可执行文件,PATH环境变量应该包含一个指向此目录的 ...

  7. 【Lucene3.6.2入门系列】第04节_中文分词器

    package com.jadyer.lucene; import java.io.IOException; import java.io.StringReader; import org.apach ...

  8. CentOS 命令随笔

     linux下敲命令时:快速删除当前行已经敲的命令: CTR+U  或者 CTR+/                         快速删除当前行刚输入接近鼠标当前位置的单词:CTR+W 以上在XS ...

  9. git commit 之后 push 之前,想删除 个别的commit 文件

    git rm --cached <file_name> git commit "删除了<file_name>文件" git rm --cached < ...

  10. Codeforces Round #221 (Div. 2) Lever I.O.U.

    这场cf 做的很差,,第一题犯了一个很低级的错误..把i写成了J.... 第二题 想的太复杂了...其实我们只需要 考虑每个人自己的负债情况就行了,就是假设每个人把别人 欠他的钱,拿过来还给别人..这 ...