iOS动画相关(持续更新)

2024-09-05 13:38
文章标签 更新 相关 ios 动画 持续

本文主要是介绍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:selfselector:@selector(addAnimation:)name:UIApplicationWillEnterForegroundNotificationobject: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:animationforKey:@"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 impliedCABasicAnimation 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 impliedCABasicAnimation 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 impliedCABasicAnimation object, described in Table B-2.

frame

This property is not animatable. You can achieve the same results by animating thebounds andposition 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动画相关(持续更新)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1139118

相关文章

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

linux安装、更新、卸载anaconda实践

《linux安装、更新、卸载anaconda实践》Anaconda是基于conda的科学计算环境,集成1400+包及依赖,安装需下载脚本、接受协议、设置路径、配置环境变量,更新与卸载通过conda命令... 目录随意找一个目录下载安装脚本检查许可证协议,ENTER就可以安装完毕之后激活anaconda安装更

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs