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

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

  • 核心动画的层次关系

  • 转场动画(CATransition)

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

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

   CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
// 位移
CAKeyframeAnimation *positionAnima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnima.calculationMode = kCAAnimationCubicPaced;
positionAnima.duration = 5;
positionAnima.fillMode = kCAFillModeForwards;
positionAnima.removedOnCompletion = NO;
positionAnima.repeatCount = MAXFLOAT;
positionAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; // 添加移动路径
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGRect circleContainer = CGRectInset(myView.frame, myView.frame.size.width / - , myView.frame.size.height / - );
CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
positionAnima.path = curvedPath;
CGPathRelease(curvedPath);
[myView.layer addAnimation:positionAnima forKey:nil]; // 缩放X
CAKeyframeAnimation *scaleX = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
scaleX.duration = 1.0;
scaleX.values = @[@1.0,@1.1,@1.0];
scaleX.keyTimes = @[@0.0,@0.5,@1.0];
scaleX.repeatCount = MAXFLOAT;
scaleX.autoreverses = YES; scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:scaleX forKey:nil]; // 缩放Y
CAKeyframeAnimation *scaleY = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
scaleY.duration = 1.5;
scaleY.values = @[@1.0,@1.1,@1.0];
scaleY.keyTimes = @[@0.0,@0.5,@1.0];
scaleY.repeatCount = MAXFLOAT;
scaleY.autoreverses = YES; group.animations = @[positionAnima,scaleX,scaleY];
scaleY.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:scaleY forKey:nil];

运行效果

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

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

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

  • UIView动画(手码)

    • 添加单个动画

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

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

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

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. HDMI介绍与流程

    HDMI接口 http://baike.c114.net/view.asp?id=17671-21565442 DDC(Display Data Channel)通道用于HDMI发送和接收端之间交换一 ...

  2. mysql查看数据库命令

    mysql查看数据库命令 打开的数据库的命令 mysql> use mysql Database changed 查看数据库的命令 mysql> show databases; 查看数据表 ...

  3. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

  4. Photoshop:路径填充边缘虚化问题

    怎么才能不让它虚化呢?  解决方案一: 1.同样画出路径 2.新建图层 3.回到路径面板,右击路径图层,选择“填充路径” 4.把“羽化”设置为0,取消选择“消除锯齿” 换个背景色看看效果:一点虚化都没 ...

  5. 解决 Your project contains error(s),please fix them before running your application问题

    原文地址: Android笔记:解决 Your project contains error(s),please fix them before running your application问题 ...

  6. ArrayList源代码深入剖析

    第1部分 ArrayList介绍ArrayList底层采用数组实现,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess, Cloneable, java. ...

  7. js setTimeout深度递归后完成回调

    setTimout原型: iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])     setTimeout有两种形式 se ...

  8. atoi函数的实现(考虑不同进制、溢出)

    2013-07-08 16:21:15 atio就是将ASCII码表示的字符为int型数据,看似简单的问题,实际上需要考虑的很多.对于简单的问题,考虑是否全面就显得特别重要. 小结: 对于该函数的实现 ...

  9. Java 关于中文乱码处理的经验总结【转载】

    为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程.汉语编程是怎么回事我也 ...

  10. SGU185 - Two Shortest

    原题地址:http://acm.sgu.ru/problem.php?contest=0&problem=185 题目大意:给出一个无向图,求出从 1 到 n 的两条没有相同边的最短路径(允许 ...