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追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个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

Oracle 通过 ROWID 批量更新表的方法

《Oracle通过ROWID批量更新表的方法》在Oracle数据库中,使用ROWID进行批量更新是一种高效的更新方法,因为它直接定位到物理行位置,避免了通过索引查找的开销,下面给大家介绍Orac... 目录oracle 通过 ROWID 批量更新表ROWID 基本概念性能优化建议性能UoTrFPH优化建议注

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException: org.junit.Test问题

《解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException:org.junit.Test问题》:本文主要介绍解决tomcat启动时报Junit相... 目录tomcat启动时报Junit相关错误Java.lang.ClassNotFoundException

Redis中6种缓存更新策略详解

《Redis中6种缓存更新策略详解》Redis作为一款高性能的内存数据库,已经成为缓存层的首选解决方案,然而,使用缓存时最大的挑战在于保证缓存数据与底层数据源的一致性,本文将介绍Redis中6种缓存更... 目录引言策略一:Cache-Aside(旁路缓存)策略工作原理代码示例优缺点分析适用场景策略二:Re

Pandas利用主表更新子表指定列小技巧

《Pandas利用主表更新子表指定列小技巧》本文主要介绍了Pandas利用主表更新子表指定列小技巧,通过创建主表和子表的DataFrame对象,并使用映射字典进行数据关联和更新,实现了从主表到子表的同... 目录一、前言二、基本案例1. 创建主表数据2. 创建映射字典3. 创建子表数据4. 更新子表的 zb