动画缓冲 - CAMediaTimingFunction

简介: 动画缓冲 - CAMediaTimingFunction

关于CAAnimation的懂画执行,我们见过的组多的都是以匀速运动的,而实际上,物体的运动速度并不是匀速的,要么一直加速,要么先加速后减速等等,为了实现这一缓冲的过程,需要设置CAAnimation的timingFunction属性,它是CAMediaTimingFunction的一个对象,如果要设置隐式动画的计时函数,就设置CATransaction的+setAnimationTimingFunction: 方法。


创建CAMediaTimingFunction最简单的方式是调用+timingFunctionWithName:的构造方法,它有以下几个值:

  kCAMediaTimingFunctionLinear//匀速的线性计时函数
  kCAMediaTimingFunctionEaseIn//缓慢加速,然后突然停止
  kCAMediaTimingFunctionEaseOut//全速开始,慢慢减速
  kCAMediaTimingFunctionEaseInEaseOut//慢慢加速再慢慢减速
  kCAMediaTimingFunctionDefault//也是慢慢加速再慢慢减速,但是它加速减速速度略慢

下面看下测试代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.colorLayer = [CALayer layer];
    self.colorLayer.frame = CGRectMake(0, 0, 100, 100);
    self.colorLayer.position = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2);
    self.colorLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:self.colorLayer];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //configure the transaction
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    //set the position
    self.colorLayer.position = [[touches anyObject] locationInView:self.view]; //commit transaction
    [CATransaction commit];
 }

在UIView中,前面有说过UIView的过度动画,它同样有以上特性,要想改变UIView的缓冲,只需要设置options的参数即可:

//跟上面的变量结尾部分相似,功能也是一样的,不再一一写出来了,请对照上面来看,唯一的不同就是没有现实默认值,为第一项
  UIViewAnimationOptionCurveEaseInOut// 默认值
  UIViewAnimationOptionCurveEaseIn
  UIViewAnimationOptionCurveEaseOut
  UIViewAnimationOptionCurveLinear

看测试代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    //create a red layer
    self.colorView = [[UIView alloc] init];
    self.colorView.bounds = CGRectMake(0, 0, 100, 100);
    self.colorView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height/2);
    self.colorView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.colorView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:1.0 delay:0.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         //set the position
                         self.colorView.center = [[touches anyObject] locationInView:self.view];
                     }
                     completion:NULL];
}

这里只是简单的运用,想要学的更多,欢迎继续关注。

目录
相关文章
|
数据可视化 Python
Matplotlib 教程 之 Matplotlib 饼图 5
使用 Matplotlib 的 `pie()` 方法绘制饼图,通过参数设置(如颜色、标签和比例等),轻松展示各类别占比。示例代码展示了如何创建一个具有突出部分的彩色饼图并显示百分比。`pie()` 方法支持多种参数定制,包括阴影、旋转角度及文本属性等。
454 3
|
数据采集 机器学习/深度学习 数据挖掘
从混乱到有序,Python数据清洗术,让你的数据分析之路畅通无阻!
【7月更文挑战第20天】数据清洗在数据分析中至关重要,它确保数据质量,影响分析准确性和效率。Python的Pandas库是数据预处理的得力工具。基本步骤包括:导入数据(如`pd.read_csv()`)、检查概况(`head()`, `info()`, `describe()`)、处理缺失值(`fillna()`或`dropna()`)、转换数据类型(`pd.to_numeric()`)、去除重复项(`drop_duplicates()`)、排序和筛选数据,以及对分类变量编码(如使用`LabelEncoder`)。
521 3
|
程序员 Android开发 Swift
iOS 音视频开发的一些基础准备工作
最近在捣鼓 iOS 上的音视频开发,由于之前并没有 iOS 开发经验,直接上手写代码的话压力还是挺大的,因此也趁机看了下 iOS 开发的内容,算是做一些准备工作吧。
611 0
iOS 音视频开发的一些基础准备工作
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8097 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10645 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全