先说下基本动画部分
使用方法大致为:
#1. 创建原始UI或者画面
#2. 创建CABasicAnimation实例, 并设置keypart/duration/fromValue/toValue
#3. 设置动画最终停留的位置
#4. 将配置好的动画添加到layer层中
举个例子, 比如实现一个圆形从上往下移动, 上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//设置原始画面
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
showView.layer.masksToBounds = YES;
showView.layer.cornerRadius = 50.f;
showView.layer.backgroundColor = [UIColor redColor].CGColor;
[self.view addSubview:showView];
//创建基本动画
CABasicAnimation *basicAnimation = [CABasicAnimation animation];
//设置属性
basicAnimation.keyPath = @ "position" ;
basicAnimation.duration = 4.0f;
basicAnimation.fromValue = [NSValue valueWithCGPoint:showView.center];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(50, 300)];
//设置动画结束位置
showView.center = CGPointMake(50, 300);
//添加动画到layer层
[showView.layer addAnimation:basicAnimation forKey:nil];
|
其实跟基本动画差不多, 只是能设置多个动画路径 使用方法也类似, 大致为
#1. 创建原始UI或者画面
#2. 创建CAKeyframeAnimation实例, 并设置keypart/duration/values 相比基本动画只能设置开始和结束点, 关键帧动画能添加多个动画路径点
#3. 设置动画最终停留的位置
#4. 将配置好的动画添加到layer层中
举个例子, 红色圆形左右晃动往下坠落 上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
//设置原始画面
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
showView.layer.masksToBounds = YES;
showView.layer.cornerRadius = 50.f;
showView.layer.backgroundColor = [UIColor redColor].CGColor;
[self.view addSubview:showView];
//创建关键帧动画
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
//设置动画属性
keyFrameAnimation.keyPath = @ "position" ;
keyFrameAnimation.duration = 4.0f;
keyFrameAnimation.values = @[[NSValue valueWithCGPoint:showView.center],
[NSValue valueWithCGPoint:CGPointMake(100, 100)],
[NSValue valueWithCGPoint:CGPointMake(50, 150)],
[NSValue valueWithCGPoint:CGPointMake(200, 200)]];
//设置动画结束位置
showView.center = CGPointMake(200, 200);
//添加动画到layer层
[showView.layer addAnimation:keyFrameAnimation forKey:nil];
|
先说说什么是缓动函数, 就是有高人写了一个库可以计算出模拟物理性动画(比如弹簧效果)所要的路径
Github地址: https://github.com/YouXianMing/EasingAnimation
具体有哪些动画效果可看库中的缓动函数查询表, 简单举个小球落地的效果
上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//设置原始画面
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
showView.layer.masksToBounds = YES;
showView.layer.cornerRadius = 50.f;
showView.layer.backgroundColor = [UIColor redColor].CGColor;
[self.view addSubview:showView];
//创建关键帧动画
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
//设置动画属性
keyFrameAnimation.keyPath = @ "position" ;
keyFrameAnimation.duration = 4.0f;
//关键处, 在这里使用的缓动函数计算点路径
keyFrameAnimation.values = [YXEasing calculateFrameFromPoint:showView.center
toPoint:CGPointMake(50, 300)
func:BounceEaseOut
frameCount:4.0f * 30];
//设置动画结束位置
showView.center = CGPointMake(50, 300);
//添加动画到layer层
[showView.layer addAnimation:keyFrameAnimation forKey:nil];
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
php通过PHPExcel导入Excel表格到MySQL数据库的简单实例
2025-05-29 55 -
2025-05-25 39
-
新手建站SEO:内容更新频率应该如何规划才能持续获得良好的搜索结果?
2025-06-04 90 -
2025-05-27 27
-
2025-05-29 34