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

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

sqlite3 相关知识

WAL 模式 VS 回滚模式 特性WAL 模式回滚模式(Rollback Journal)定义使用写前日志来记录变更。使用回滚日志来记录事务的所有修改。特点更高的并发性和性能;支持多读者和单写者。支持安全的事务回滚,但并发性较低。性能写入性能更好,尤其是读多写少的场景。写操作会造成较大的性能开销,尤其是在事务开始时。写入流程数据首先写入 WAL 文件,然后才从 WAL 刷新到主数据库。数据在开始

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

AI行业应用(不定期更新)

ChatPDF 可以让你上传一个 PDF 文件,然后针对这个 PDF 进行小结和提问。你可以把各种各样你要研究的分析报告交给它,快速获取到想要知道的信息。https://www.chatpdf.com/

GIS图形库更新2024.8.4-9.9

更多精彩内容请访问 dt.sim3d.cn ,关注公众号【sky的数孪技术】,技术交流、源码下载请添加微信:digital_twin123 Cesium 本期发布了1.121 版本。重大新闻,Cesium被Bentley收购。 ✨ 功能和改进 默认启用 MSAA,采样 4 次。若要关闭 MSAA,则可以设置scene.msaaSamples = 1。但是通过比较,发现并没有多大改善。

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。