1.When my application is entering background, because the user push the home button, the animations correctly set in pause, but when i re-open my app, the animations have disappeard.How could i fix it please ?

当我的应用进入了后台,因为用户按了home键,动画被设置成了暂停,但当我重新打开应用时,动画都消失了,我如何修复它?

This is correct and built-in behavior. When you leave the app, all animations are removed from their layers: the system calls removeAllAnimations on every layer.

你的情况是系统默认的行为.当你离开了应用后(比如进入了后台),所有的动画都从他们的layer上移除了:因为系统调用了removeAllAnimations,针对所有的layer.

附录:

UIViewController中的view显示步骤

--------------------------------------------------------------------------------------------------------

进入UIViewController时的情况:

viewDidLoad
viewWillLayoutSubviews
viewDidLayoutSubviews
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear

切换了Controller后的情况(比如你在TabbarController中切换了):

viewWillDisappear
viewDidDisappear

再次切换回来后的情况:
viewWillLayoutSubviews
viewDidLayoutSubviews
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear

退入到后台后的情况:

从后台进入程序时的情况:

viewWillLayoutSubviews
viewDidLayoutSubviews

--------------------------------------------------------------------------------------------------------

为了解决从后台切换回来或者从TabbarController切换回来动画还能继续动画效果,需要如下的解决方案:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; // 添加通知(处理从后台进来后的情况)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(addAnimation:)
name:UIApplicationWillEnterForegroundNotification
object:nil]; // 添加动画的代码
}
- (void)addAnimation:(NSNotification *)notificaiton
{
   // 添加动画的代码
}

2.基本动画类型

旋转动画

    /* 旋转 */
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; // 一次完整的动画所持续的时间
animation.duration = .f; // 重复次数
animation.repeatCount = HUGE_VALF; // 起始角度
animation.fromValue = [NSNumber numberWithFloat:0.0]; // 终止角度
animation.toValue = [NSNumber numberWithFloat:- * M_PI]; // 添加动画
[_showView.layer addAnimation:animation
forKey:@"rotate-layer"];

透明度

    // 透明度动画
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"]; // 初始值
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0]; // 结束值
fadeAnim.toValue = [NSNumber numberWithFloat:0.0]; // 动画持续一次的时间
fadeAnim.duration = 1.0; // 开始动画
[_showView.layer addAnimation:fadeAnim forKey:@"opacity"]; // 无论动画是否被中断,其最终的值还是被设置过了
_showView.layer.opacity = 0.0;

borderWidth动画

    // borderWidth动画
CABasicAnimation *borderWidthAnimation = \
[CABasicAnimation animationWithKeyPath:@"borderWidth"]; // 初始值
borderWidthAnimation.fromValue = [NSNumber numberWithFloat:0.0]; // 结束值
borderWidthAnimation.toValue = [NSNumber numberWithFloat:3.0]; // 动画持续一次的时间
borderWidthAnimation.duration = .f; // 开始动画
[_showView.layer addAnimation:borderWidthAnimation forKey:@"borderWidth"]; // 无论动画是否被中断,其最终的值还是被设置过了
_showView.layer.borderWidth = 3.0f;

backgroundColor动画

    // backgroundColor动画
CABasicAnimation *borderWidthAnimation = \
[CABasicAnimation animationWithKeyPath:@"backgroundColor"]; // 初始值
borderWidthAnimation.fromValue = (id)[[UIColor redColor] CGColor]; // 结束值
borderWidthAnimation.toValue = (id)[[UIColor greenColor] CGColor]; // 动画持续一次的时间
borderWidthAnimation.duration = .f; // 开始动画
[_showView.layer addAnimation:borderWidthAnimation forKey:@"borderWidth"]; // 无论动画是否被中断,其最终的值还是被设置过了
_showView.layer.backgroundColor = [[UIColor greenColor] CGColor];

borderColor动画

    // borderColor动画
CABasicAnimation *borderWidthAnimation = \
[CABasicAnimation animationWithKeyPath:@"borderColor"]; // 初始值
borderWidthAnimation.fromValue = (id)[[UIColor redColor] CGColor]; // 结束值
borderWidthAnimation.toValue = (id)[[UIColor greenColor] CGColor]; // 动画持续一次的时间
borderWidthAnimation.duration = .f; // 开始动画
[_showView.layer addAnimation:borderWidthAnimation forKey:@"borderWidth"]; // 无论动画是否被中断,其最终的值还是被设置过了
_showView.layer.backgroundColor = [[UIColor greenColor] CGColor];

bounds.size.height动画

    // bounds.size.height动画
CABasicAnimation *borderWidthAnimation = \
[CABasicAnimation animationWithKeyPath:@"bounds.size.height"]; // 初始值
borderWidthAnimation.fromValue = [NSNumber numberWithFloat:100.0f]; // 结束值
borderWidthAnimation.toValue = [NSNumber numberWithFloat:.f]; // 动画持续一次的时间
borderWidthAnimation.duration = 0.5f; // 选择一种动画的时间轴方式
borderWidthAnimation.timingFunction = \
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; // ??
borderWidthAnimation.fillMode = kCAFillModeForwards; // 开始动画
[_showView.layer addAnimation:borderWidthAnimation forKey:@"bounds.size.height"]; // 无论动画是否被中断,其最终的值还是被设置过了
_showView.layer.bounds = CGRectMake(self.view.center.x, self.view.center.y, , .f);

contents动画

    // 初始化一张图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
imageView.image = [UIImage imageNamed:@""]; // 添加进view中
[self.view addSubview:imageView]; // contents动画
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"]; crossFade.duration = 2.0;
crossFade.fromValue = (id)([UIImage imageNamed:@""].CGImage);
crossFade.toValue = (id)([UIImage imageNamed:@""].CGImage); [imageView.layer addAnimation:crossFade forKey:@"animateContents"]; // 进行最后的设置
imageView.image = [UIImage imageNamed:@""];

圆角动画

    // 初始化一张图片
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
showView.backgroundColor = [UIColor redColor];
[self.view addSubview:showView]; // 圆角动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"]; animation.fromValue = [NSNumber numberWithFloat:.f];
animation.toValue = [NSNumber numberWithFloat:.f];
animation.duration = 1.0;
[showView.layer setCornerRadius:.f]; // 最后设置
[showView.layer addAnimation:animation forKey:@"cornerRadius"];

支持的动画太多了,以下是苹果的官方文档中提出的支持的动画:

Property

Default animation

anchorPoint

Uses the default implied CABasicAnimation object, described in Table B-2.

backgroundColor

Uses the default implied CABasicAnimation object, described in Table B-2.

backgroundFilters

Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.

borderColor

Uses the default implied CABasicAnimation object, described in Table B-2.

borderWidth

Uses the default implied CABasicAnimation object, described in Table B-2.

bounds

Uses the default implied CABasicAnimation object, described in Table B-2.

compositingFilter

Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.

contents

Uses the default implied CABasicAnimation object, described in Table B-2.

contentsRect

Uses the default implied CABasicAnimation object, described in Table B-2.

cornerRadius

Uses the default implied CABasicAnimation object, described in Table B-2.

doubleSided

There is no default implied animation.

filters

Uses the default implied CABasicAnimation object, described in Table B-2. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.

frame

This property is not animatable. You can achieve the same results by animating the bounds and position properties.

hidden

Uses the default implied CABasicAnimation object, described in Table B-2.

mask

Uses the default implied CABasicAnimation object, described in Table B-2.

masksToBounds

Uses the default implied CABasicAnimation object, described in Table B-2.

opacity

Uses the default implied CABasicAnimation object, described in Table B-2.

position

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowColor

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowOffset

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowOpacity

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowPath

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowRadius

Uses the default implied CABasicAnimation object, described in Table B-2.

sublayers

Uses the default implied CABasicAnimation object, described in Table B-2.

sublayerTransform

Uses the default implied CABasicAnimation object, described in Table B-2.

transform

Uses the default implied CABasicAnimation object, described in Table B-2.

zPosition

Uses the default implied CABasicAnimation object, described in Table B-2.

http://www.cnblogs.com/pengyingh/articles/2379631.html

iOS动画相关(持续更新)的更多相关文章

  1. 移动端H5制作安卓和IOS的坑 持续更新...

    移动端H5制作安卓和IOS的坑 持续更新... 前言:最近参加公司的H5页面创意竞赛,又遇到不少页面在不同系统上的坑.踩坑之余,觉得很多之前遇到的知识点都忘了,索性开一篇博文,把这些坑都统一归纳起来, ...

  2. 爆炸!iOS资源大礼包(持续更新...)

    今天为大家整理了一些关于iOS学习的干货,献给正在奋斗的你们,首先声明一下,在整理的过程中参考了大量的博客和文章,知识的分享终究会增值,在此表示感谢,希望这篇文章给大家带来帮助. 基础部分: C语言教 ...

  3. <精华篇>:iOS视频大全-持续更新

    注意:新浪微博分享的资料和简书分享的资料,略有不同! 小码哥swift3.0版 斗鱼项目视频:点击下载  iOS开发25个项目实战:点击下载 2016PHP全套下载:点击下载  黑马刀哥iOS视频精选 ...

  4. iOS Debug心得 (持续更新)

    最近在维护一个内部比较混乱的APP,Debug的时候遇到很多比较痛苦的地方, 因此做一个Debug记录,对以后的开发会有比较大的帮助: 这样,在开发新项目的时候就可以争取把一些BUG扼杀在襁褓中. & ...

  5. iOS开发资源(持续更新)

    vm10虚拟机安装Mac OS X10.10教程 马上着手开发 iOS 应用程序 (Start Developing iOS Apps Today) Xcode使用教程详细讲解 (上) Xcode使用 ...

  6. iOS 常用三方(持续更新)

    iOS 常用三方 1.ZWMSegmentController 分页控制器 https://github.com/weiming4219/ZWMSegmentController

  7. css3布局相关(持续更新)

    1三栏布局,两边定宽,中间自适应 2让文字位于div元素的正中央 3不管浏览器窗口如何变化,让一张图片始终显示在浏览器正中央.

  8. JS 字符串处理相关(持续更新)

    一.JS判断字符串中是否包含某个字符串 indexOf() indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置.如果要检索的字符串值没有出现,则该方法返回 -1. var str ...

  9. 常用的iOS 宏定义 (持续更新中)

    1.System Versioning Preprocessor Macros #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevic ...

随机推荐

  1. ***四种参数传递的形式——URL,超链接,js,form表单

    什么时候用GET,  查,删 什么时候用POST,增,改  (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http:// ...

  2. #NodeJS 服务器基本模板

    基本server配置 cookie / session / get数据 / post数据 / 请求方法 const express=require('express'); const static=r ...

  3. bash101总结

    看了 bash101 ,做的一些总结吧,都是些常见用法,易错 1. 有空格会显示多行 2.contine 书里太细了,有空补起来

  4. 【LOJ】#2027. 「SHOI2016」黑暗前的幻想乡

    题解 我一开始写的最小表示法写的插头dp,愉快地TLE成60分 然后我觉得我就去看正解了! 发现是容斥 + 矩阵树定理 矩阵树定理对于有重边的图只要邻接矩阵的边数设置a[u][v]表示u,v之间有几条 ...

  5. const理解

    const int * a4 = &a1; ///const data,non-const pointer int * const a5 = &a1; ///non-const dat ...

  6. matplotlib使用总结

    一.简介 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图 ...

  7. Vue.js中 watch 的高级用法

    假设有如下代码: <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type=&q ...

  8. php实现微信分享朋友圈

    class JSSDK {  private $appId;  private $appSecret; public function __construct($appId, $appSecret) ...

  9. html5+css3 h5页面生成的思路

    <!DOCTYPE html><html style="height: 100%;"> <head> <meta charset=&quo ...

  10. js根据IP跳转

    <script language="javascript" type="text/javascript" src="http://int.dpo ...