Tween缓动动画在ui中大量的使用,根据缓动算法可以很快的创造出,预定的缓冲运动效果。其核心的概念就是,给出初始值,目标值,时间。然后差值之间每个时间点上的数值变化,从而得到一组运动轨迹。
我实现的Tween通用结构,主要分为3个部分,每个部分单独一篇文章来解释。
第一, 插值算法公式。根据初始值,目标值,和当前时间,来计算当前属性值。
第二, 定义Tween执行状态的各种属性,队列调用,回调函数等数据结构及算法。
第三,在Tween之上,封装一个快捷调用的使用接口。
首先,插值算法公式有预定义的一套算法Robert Penner's Easing Functions,几乎包含了常用的所有套路。当然,也可以再这个模式之上定义自己的算法公式。这里给出,我封装的代码。
/*
*
*
* Created on: 2013-11-5
* Author: scott.cgi
*/
#ifndef tween_ease_h_
#define tween_ease_h_
typedef enum
{
tween_ease_linear,
/* quadratic */
tween_ease_quad_in,
tween_ease_quad_out,
tween_ease_quad_both,
/* cubic */
tween_ease_cubic_in,
tween_ease_cubic_out,
tween_ease_cubic_both,
/* quartic */
tween_ease_quart_in,
tween_ease_quart_out,
tween_ease_quart_both,
/* quintic */
tween_ease_quint_in,
tween_ease_quint_out,
tween_ease_quint_both,
/* sine */
tween_ease_sine_in,
tween_ease_sine_out,
tween_ease_sine_both,
/* exponential */
tween_ease_expo_in,
tween_ease_expo_out,
tween_ease_expo_both,
/* circular */
tween_ease_circ_in,
tween_ease_circ_out,
tween_ease_circ_both,
/* elastic */
tween_ease_elastic_in,
tween_ease_elastic_out,
tween_ease_elastic_both,
/* back */
tween_ease_back_in,
tween_ease_back_out,
tween_ease_back_both,
/* bounce */
tween_ease_bounce_in,
tween_ease_bounce_out,
tween_ease_bounce_both,
tween_ease_type_length,
}
TweenEaseType;
typedef struct
{
/**
* Ease interpolation algorithm function
* value between [from, to] control by time between [0.0f, 1.0f]
*/
float (*interpolates[tween_ease_type_length])(float from, float to, float time);
}
_ATweenEase_;
extern _ATweenEase_ ATweenEase[1];