iOS动画相关(持续更新)

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 = 1.f;

    // 重复次数
    animation.repeatCount = HUGE_VALF;

    // 起始角度
    animation.fromValue = [NSNumber numberWithFloat:0.0];

    // 终止角度
    animation.toValue   = [NSNumber numberWithFloat:- 2 * 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    = 1.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    = 1.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    = 1.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:300.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, 20, 300.f);

contents动画

// 初始化一张图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.image = [UIImage imageNamed:@"1"];

    // 添加进view中
    [self.view addSubview:imageView];

    // contents动画
    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];

    crossFade.duration = 2.0;
    crossFade.fromValue  = (id)([UIImage imageNamed:@"1"].CGImage);
    crossFade.toValue    = (id)([UIImage imageNamed:@"2"].CGImage);

    [imageView.layer addAnimation:crossFade forKey:@"animateContents"];

    // 进行最后的设置
    imageView.image = [UIImage imageNamed:@"2"];

圆角动画

// 初始化一张图片
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    showView.backgroundColor = [UIColor redColor];
    [self.view addSubview:showView];

    // 圆角动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];

    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue   = [NSNumber numberWithFloat:30.f];
    animation.duration  = 1.0;
    [showView.layer setCornerRadius:30.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

时间: 2024-08-29 08:37:46

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

iOS内存优化(持续更新)

   在iphone开发过程中,代码中的内存泄露我们很容易用内存检测工具leaks 检测出来,并一一改之,但有些是因为ios 的缺陷和用法上的错误,leaks 检测工具并不能检测出来,你只会看到大量的内存被使用,最后收到didReceiveMemoryWarning,最终导致程序崩溃.以下是开发过程中遇到的一些问题和网上的一些资料,总结了一下: UIImage如何加载图片 用UIImage加载本地图像最常用的是下面三种: 1.imageNamed UIImage *image = [UIImag

iOS 文件及字符串相关操作汇总 - 持续更新中......

iOS 文件及字符串相关操作汇总 - 持续更新中...... 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 1.获取应用包中文件的绝对路径: NSString *absoluteFileNam

IOS开发--常用工具类收集整理(Objective-C)(持续更新)

 前言:整理和收集了IOS项目开发常用的工具类,最后也给出了源码下载链接. 这些可复用的工具,一定会给你实际项目开发工作锦上添花,会给你带来大大的工作效率. 重复造轮子的事情,除却自我多练习编码之外,就不要傻傻的重复造轮子了,还是提高工作效率,早点完成工作早点回家陪老婆孩子. 所以下面备份的常用工具类一定是你需要的. 前提:你有一定的开发经验,知道它们在开发的什么地方需要,你都不知道用在哪里,那你需要个毛啊,还是好好另外学好基础吧.少儿不宜,请离开哦. 插件目录列表:(持续更新和添加) 1.UI

iOS之github第三方框架(持续更新)

1.MBProgressHUD MBProgressHUD是一个开源项目,实现了很多种样式的提示框 使用上简单.方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到. 到Github上可以下载到项目源码https://github.com/jdg/MBProgressHUD 百度云链接: http://pan.baidu.com/s/1pKa5JrP 密码: qw6k 百度云链接: http://pan.baidu.com/s/1dErLfrz 密码: ixbn (小码哥进一步

给大家推荐几款个人觉得特别好用的编程及日常用的软件(持续更新!)

原文:给大家推荐几款个人觉得特别好用的编程及日常用的软件(持续更新!) 子曾经曰过:"工欲善其事,必先利其器!",呵呵,好用的软件必然会对工作和生活提高很多效率,下面我就把我个人觉得不错的软件整理出来,这里可能之前提到过几个,不过没关系,以后我再发现好用的东西会在这里持续更新! 宝典总数:[27个]  更新时间:[2015年1月29日09:04:21] ※宝典1:源代码管理软件-"CODEHELP" CodeHelp是专门为我们程序员设计的一款源代码管理软件.它能方

300+篇运维、数据库等实战资料免费下载(文章+PDF+视频,持续更新)

2017年已过去一半,在此小编为大家精心整理了2017上半年热点事件解析.实战技术资料以及特别策划短视频系列,希望可以帮助大家更深入地回顾上半年的技术热点,并储备更充足的技术干粮继续2017的下一半. PART 1 峰会回顾资料 云栖大会 [上海云栖大会]2017云栖大会上海峰会资料合计(现场视频+PDF下载) [成都云栖大会]2017云栖大会成都峰会资料合计(现场视频+PDF下载) [南京云栖大会]2017云栖大会南京峰会资料合计(现场视频+PDF下载) 技术峰会 [运维/DevOps峰会]

阿里巴巴大数据计算平台MaxCompute(原名ODPS)全套攻略(持续更新20171122)

  概况介绍 大数据计算服务(MaxCompute,原名ODPS,产品地址:https://www.aliyun.com/product/odps)是一种快速.完全托管的TB/PB级数据仓库解决方案.MaxCompute向用户提供了完善的数据导入方案以及多种经典的分布式计算模型,能够更快速的解决用户海量数据计算问题,有效降低企业成本,并保障数据安全.本文收录了大量的MaxCompute产品介绍.技术介绍,帮助您快速了解MaxCompute/ODPS. MaxCompute 2.0:阿里巴巴的大数

seo问答交流(持续更新中)

通过问答交流的形式,可以帮助我们提升对于seo技术的认知,更深刻的理解相关概念,解答疑惑.以持续更新的方式,解答常见的网站seo优化问题,笔者会第一时间回复所有问题.seo问答交流详细内容有很多,请仔细阅读: seo问答交流 1,问:如何查询移动端网站的收录? 答:通过百度站长平台验证网站,通过后台的索引量查询收录,较为准确. 2,问:每天都在更新文章,但是不收录? 答:可以用排除法,网站是否被搜索引擎蜘蛛爬取过:是否为处于审核期的新站点:网站是否由于过度seo优化,被黑等问题处于降权中:更新的

Android LookAround开元之旅(持续更新中...)

http://blog.csdn.net/lancees/article/details/17696805 应用介绍 随便瞧瞧是一款为android用户量身定做的免费图文资讯软件 集美食,星座,写真,文学,美女等频道于一体 界面简洁,操作流畅,图文分享,个性收藏 是广大卓粉的必备神器 APK下载 -->http://apk.91.com/Soft/Detail.aspx?Platform=Android&f_id=40495952         工程简介以下是代码结构图: 该工程用到以下开