支持xcode6的缓动函数Easing以及使用示例

用xcode6新建工程后,直接导致不支持之前的Easing缓动函数的代码,经过修改后就可以正常使用了,虽然比不上POP高大上的动画,但用缓动函数的动画还是能够实现很复杂的效果的。

注:Easing缓动函数服务于关键帧动画,理解这一点很重要,需要你对CoreAnimation有着很深入的了解才能够用得得心应手

提供源码如下:

Easing.h 与 Easing.m

//
// Easing.h
// Easing
//
// Created by YouXianMing on 14-10-10.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #ifndef __Easing__Easing__
#define __Easing__Easing__ #include <stdio.h>
#include <math.h> #if defined(__LP64__) && !defined(AH_EASING_USE_DBL_PRECIS)
#define AH_EASING_USE_DBL_PRECIS
#endif #ifdef AH_EASING_USE_DBL_PRECIS
#define AHFloat double
#else
#define AHFloat float
#endif // 函数指针
typedef AHFloat (*AHEasingFunction)(AHFloat); // Linear interpolation (no easing)
AHFloat LinearInterpolation(AHFloat p); // Quadratic easing; p^2
AHFloat QuadraticEaseIn(AHFloat p);
AHFloat QuadraticEaseOut(AHFloat p);
AHFloat QuadraticEaseInOut(AHFloat p); // Cubic easing; p^3
AHFloat CubicEaseIn(AHFloat p);
AHFloat CubicEaseOut(AHFloat p);
AHFloat CubicEaseInOut(AHFloat p); // Quartic easing; p^4
AHFloat QuarticEaseIn(AHFloat p);
AHFloat QuarticEaseOut(AHFloat p);
AHFloat QuarticEaseInOut(AHFloat p); // Quintic easing; p^5
AHFloat QuinticEaseIn(AHFloat p);
AHFloat QuinticEaseOut(AHFloat p);
AHFloat QuinticEaseInOut(AHFloat p); // Sine wave easing; sin(p * PI/2)
AHFloat SineEaseIn(AHFloat p);
AHFloat SineEaseOut(AHFloat p);
AHFloat SineEaseInOut(AHFloat p); // Circular easing; sqrt(1 - p^2)
AHFloat CircularEaseIn(AHFloat p);
AHFloat CircularEaseOut(AHFloat p);
AHFloat CircularEaseInOut(AHFloat p); // Exponential easing, base 2
AHFloat ExponentialEaseIn(AHFloat p);
AHFloat ExponentialEaseOut(AHFloat p);
AHFloat ExponentialEaseInOut(AHFloat p); // Exponentially-damped sine wave easing
AHFloat ElasticEaseIn(AHFloat p);
AHFloat ElasticEaseOut(AHFloat p);
AHFloat ElasticEaseInOut(AHFloat p); // Overshooting cubic easing;
AHFloat BackEaseIn(AHFloat p);
AHFloat BackEaseOut(AHFloat p);
AHFloat BackEaseInOut(AHFloat p); // Exponentially-decaying bounce easing
AHFloat BounceEaseIn(AHFloat p);
AHFloat BounceEaseOut(AHFloat p);
AHFloat BounceEaseInOut(AHFloat p); #endif /* defined(__Easing__Easing__) */
//
// Easing.c
// Easing
//
// Created by YouXianMing on 14-10-10.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #include "Easing.h" // Modeled after the line y = x
AHFloat LinearInterpolation(AHFloat p)
{
return p;
} // Modeled after the parabola y = x^2
AHFloat QuadraticEaseIn(AHFloat p)
{
return p * p;
} // Modeled after the parabola y = -x^2 + 2x
AHFloat QuadraticEaseOut(AHFloat p)
{
return -(p * (p - ));
} // Modeled after the piecewise quadratic
// y = (1/2)((2x)^2) ; [0, 0.5)
// y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
AHFloat QuadraticEaseInOut(AHFloat p)
{
if(p < 0.5)
{
return * p * p;
}
else
{
return (- * p * p) + ( * p) - ;
}
} // Modeled after the cubic y = x^3
AHFloat CubicEaseIn(AHFloat p)
{
return p * p * p;
} // Modeled after the cubic y = (x - 1)^3 + 1
AHFloat CubicEaseOut(AHFloat p)
{
AHFloat f = (p - );
return f * f * f + ;
} // Modeled after the piecewise cubic
// y = (1/2)((2x)^3) ; [0, 0.5)
// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
AHFloat CubicEaseInOut(AHFloat p)
{
if(p < 0.5)
{
return * p * p * p;
}
else
{
AHFloat f = (( * p) - );
return 0.5 * f * f * f + ;
}
} // Modeled after the quartic x^4
AHFloat QuarticEaseIn(AHFloat p)
{
return p * p * p * p;
} // Modeled after the quartic y = 1 - (x - 1)^4
AHFloat QuarticEaseOut(AHFloat p)
{
AHFloat f = (p - );
return f * f * f * ( - p) + ;
} // Modeled after the piecewise quartic
// y = (1/2)((2x)^4) ; [0, 0.5)
// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
AHFloat QuarticEaseInOut(AHFloat p)
{
if(p < 0.5)
{
return * p * p * p * p;
}
else
{
AHFloat f = (p - );
return - * f * f * f * f + ;
}
} // Modeled after the quintic y = x^5
AHFloat QuinticEaseIn(AHFloat p)
{
return p * p * p * p * p;
} // Modeled after the quintic y = (x - 1)^5 + 1
AHFloat QuinticEaseOut(AHFloat p)
{
AHFloat f = (p - );
return f * f * f * f * f + ;
} // Modeled after the piecewise quintic
// y = (1/2)((2x)^5) ; [0, 0.5)
// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
AHFloat QuinticEaseInOut(AHFloat p)
{
if(p < 0.5)
{
return * p * p * p * p * p;
}
else
{
AHFloat f = (( * p) - );
return 0.5 * f * f * f * f * f + ;
}
} // Modeled after quarter-cycle of sine wave
AHFloat SineEaseIn(AHFloat p)
{
return sin((p - ) * M_PI_2) + ;
} // Modeled after quarter-cycle of sine wave (different phase)
AHFloat SineEaseOut(AHFloat p)
{
return sin(p * M_PI_2);
} // Modeled after half sine wave
AHFloat SineEaseInOut(AHFloat p)
{
return 0.5 * ( - cos(p * M_PI));
} // Modeled after shifted quadrant IV of unit circle
AHFloat CircularEaseIn(AHFloat p)
{
return - sqrt( - (p * p));
} // Modeled after shifted quadrant II of unit circle
AHFloat CircularEaseOut(AHFloat p)
{
return sqrt(( - p) * p);
} // Modeled after the piecewise circular function
// y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
// y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
AHFloat CircularEaseInOut(AHFloat p)
{
if(p < 0.5)
{
return 0.5 * ( - sqrt( - * (p * p)));
}
else
{
return 0.5 * (sqrt(-(( * p) - ) * (( * p) - )) + );
}
} // Modeled after the exponential function y = 2^(10(x - 1))
AHFloat ExponentialEaseIn(AHFloat p)
{
return (p == 0.0) ? p : pow(, * (p - ));
} // Modeled after the exponential function y = -2^(-10x) + 1
AHFloat ExponentialEaseOut(AHFloat p)
{
return (p == 1.0) ? p : - pow(, - * p);
} // Modeled after the piecewise exponential
// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
AHFloat ExponentialEaseInOut(AHFloat p)
{
if(p == 0.0 || p == 1.0) return p; if(p < 0.5)
{
return 0.5 * pow(, ( * p) - );
}
else
{
return -0.5 * pow(, (- * p) + ) + ;
}
} // Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
AHFloat ElasticEaseIn(AHFloat p)
{
return sin( * M_PI_2 * p) * pow(, * (p - ));
} // Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1
AHFloat ElasticEaseOut(AHFloat p)
{
return sin(- * M_PI_2 * (p + )) * pow(, - * p) + ;
} // Modeled after the piecewise exponentially-damped sine wave:
// y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
// y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
AHFloat ElasticEaseInOut(AHFloat p)
{
if(p < 0.5)
{
return 0.5 * sin( * M_PI_2 * ( * p)) * pow(, * (( * p) - ));
}
else
{
return 0.5 * (sin(- * M_PI_2 * (( * p - ) + )) * pow(, - * ( * p - )) + );
}
} // Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
AHFloat BackEaseIn(AHFloat p)
{
return p * p * p - p * sin(p * M_PI);
} // Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
AHFloat BackEaseOut(AHFloat p)
{
AHFloat f = ( - p);
return - (f * f * f - f * sin(f * M_PI));
} // Modeled after the piecewise overshooting cubic function:
// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
AHFloat BackEaseInOut(AHFloat p)
{
if(p < 0.5)
{
AHFloat f = * p;
return 0.5 * (f * f * f - f * sin(f * M_PI));
}
else
{
AHFloat f = ( - (*p - ));
return 0.5 * ( - (f * f * f - f * sin(f * M_PI))) + 0.5;
}
} AHFloat BounceEaseIn(AHFloat p)
{
return - BounceEaseOut( - p);
} AHFloat BounceEaseOut(AHFloat p)
{
if(p < /11.0)
{
return ( * p * p)/16.0;
}
else if(p < /11.0)
{
return (/40.0 * p * p) - (/10.0 * p) + /5.0;
}
else if(p < /10.0)
{
return (/361.0 * p * p) - (/1805.0 * p) + /1805.0;
}
else
{
return (/5.0 * p * p) - (/25.0 * p) + /25.0;
}
} AHFloat BounceEaseInOut(AHFloat p)
{
if(p < 0.5)
{
return 0.5 * BounceEaseIn(p*);
}
else
{
return 0.5 * BounceEaseOut(p * - ) + 0.5;
}
}

进一步封装的面向对象的使用代码

YXEasing.m 与 YXEasing.h

//
// YXEasing.h
// Prize
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "Easing.h" /*--------------------------------------------- -动画简单的解析- BackEase :在某一动画开始沿指示的路径进行动画处理前稍稍收回该动画的移动。
BounceEase :创建弹跳效果。
CircleEase :创建使用循环函数加速和/或减速的动画。
CubicEase :创建使用公式 f(t) = t^3 加速和/或减速的动画。
ElasticEase :创建类似于弹簧在停止前来回振荡的动画。
ExponentialEase :创建使用指数公式加速和/或减速的动画。
PowerEase :创建使用公式 f(t) = t^p(其中,p 等于 Power 属性)加速和/或减速的动画。
QuadraticEase :创建使用公式 f(t) = t^2 加速和/或减速的动画。
QuarticEase :创建使用公式 f(t) = t^4 加速和/或减速的动画。
QuinticEase :创建使用公式 f(t) = t^5 加速和/或减速的动画。
SineEase :创建使用正弦公式加速和/或减速的动画。 LinearInterpolation QuadraticEaseIn
QuadraticEaseOut
QuadraticEaseInOut CubicEaseIn
CubicEaseOut
CubicEaseInOut QuarticEaseIn
QuarticEaseOut
QuarticEaseInOut QuinticEaseIn
QuinticEaseOut
QuinticEaseInOut SineEaseIn
SineEaseOut
SineEaseInOut CircularEaseIn
CircularEaseOut
CircularEaseInOut ExponentialEaseIn
ExponentialEaseOut
ExponentialEaseInOut ElasticEaseIn
ElasticEaseOut
ElasticEaseInOut BackEaseIn
BackEaseOut
BackEaseInOut BounceEaseIn
BounceEaseOut
BounceEaseInOut
---------------------------------------------*/ /*
// 计算好起始值,结束值
CGFloat oldValue = 0.f;
CGFloat newValue = 1.f; // 关键帧动画
CAKeyframeAnimation *animation = \
[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; // 设置值
[animation setValues:[YXEasing calculateFrameFromValue:oldValue
toValue:newValue
func:ElasticEaseOut
frameCount:500]]; // 设置持续时间
animation.duration = 0.5f; // 每秒增加的角度(设定结果值,在提交动画之前执行)
layer.transform = \
CATransform3DMakeRotation(newValue, 0.0, 0.0, 1.0); // 提交动画
[layer addAnimation:animation forKey:nil]; */ @interface YXEasing : NSObject // 数组中存储的数据为 NSNumber float 型
+ (NSArray *)calculateFrameFromValue:(CGFloat)fromValue
toValue:(CGFloat)toValue
func:(AHEasingFunction)func
frameCount:(size_t)frameCount; // 数组中存储的数据为 NSValue CGPoint 型
+ (NSArray *)calculateFrameFromPoint:(CGPoint)fromPoint
toPoint:(CGPoint)toPoint
func:(AHEasingFunction)func
frameCount:(size_t)frameCount; // 数组中存储的数据为 NSValue CGSize 型
+ (NSArray *)calculateFrameFromSize:(CGSize)fromSize
toSize:(CGSize)toSize
func:(AHEasingFunction)func
frameCount:(size_t)frameCount; @end
//
// YXEasing.m
// Prize
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "YXEasing.h" @implementation YXEasing + (NSArray *)calculateFrameFromValue:(CGFloat)fromValue
toValue:(CGFloat)toValue
func:(AHEasingFunction)func
frameCount:(size_t)frameCount
{
// 设置帧数量
NSMutableArray *values = [NSMutableArray arrayWithCapacity:frameCount]; // 计算并存储
CGFloat t = 0.0;
CGFloat dt = 1.0 / (frameCount - );
for(size_t frame = ; frame < frameCount; ++frame, t += dt)
{
// 此处就会根据不同的函数计算出不同的值达到不同的效果
CGFloat value = fromValue + func(t) * (toValue - fromValue); // 将计算结果存储进数组中
[values addObject:[NSNumber numberWithFloat:(float)value]];
} // 数组中存储的数据为 NSNumber float 型
return values;
} + (NSArray *)calculateFrameFromPoint:(CGPoint)fromPoint
toPoint:(CGPoint)toPoint
func:(AHEasingFunction)func
frameCount:(size_t)frameCount
{
// 设置帧数量
NSMutableArray *values = [NSMutableArray arrayWithCapacity:frameCount]; // 计算并存储
CGFloat t = 0.0;
CGFloat dt = 1.0 / (frameCount - );
for(size_t frame = ; frame < frameCount; ++frame, t += dt)
{
// 此处就会根据不同的函数计算出不同的值达到不同的效果
CGFloat x = fromPoint.x + func(t) * (toPoint.x - fromPoint.x);
CGFloat y = fromPoint.y + func(t) * (toPoint.y - fromPoint.y); // 将计算结果存储进数组中
[values addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
} // 数组中存储的数据为 NSValue CGPoint 型
return values;
} + (NSArray *)calculateFrameFromSize:(CGSize)fromSize
toSize:(CGSize)toSize
func:(AHEasingFunction)func
frameCount:(size_t)frameCount
{
// 设置帧数量
NSMutableArray *values = [NSMutableArray arrayWithCapacity:frameCount]; // 计算并存储
CGFloat t = 0.0;
CGFloat dt = 1.0 / (frameCount - );
for(size_t frame = ; frame < frameCount; ++frame, t += dt)
{
// 此处就会根据不同的函数计算出不同的值达到不同的效果
CGFloat w = fromSize.width + func(t) * (toSize.width - fromSize.width);
CGFloat h = fromSize.height + func(t) * (toSize.height - fromSize.height); // 将计算结果存储进数组中
[values addObject:[NSValue valueWithCGSize:CGSizeMake(w, h)]];
} // 数组中存储的数据为 NSValue CGSize 型
return values;
} @end

使用示例:

//
// ViewController.m
// Easing
//
// Created by YouXianMing on 14-10-10.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "YXEasing.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
showView.backgroundColor = [UIColor redColor];
showView.center = self.view.center;
[self.view addSubview:showView]; // 计算好起始值,结束值
CGFloat oldValue = .f;
CGFloat newValue = .f; // 关键帧动画
CAKeyframeAnimation *animation = \
[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; // 设置值
[animation setValues:[YXEasing calculateFrameFromValue:oldValue
toValue:newValue
func:ElasticEaseOut
frameCount:]]; // 设置持续时间
animation.duration = 1.5f; // 每秒增加的角度(设定结果值,在提交动画之前执行)
showView.layer.transform = \
CATransform3DMakeRotation(newValue, 0.0, 0.0, 1.0); // 提交动画
[showView.layer addAnimation:animation forKey:nil]; } @end

效果如下图:

支持xcode6的缓动函数Easing以及使用示例的更多相关文章

  1. GSAP JS基础教程--使用缓动函数

    今天来了解一下缓动easeing函数. 开始,如果你还没有GSAP的类包,可以到GreenSock的官网去下载最新版本的类包,或者直接点击这里​来下载 学习之前,先来准备一下:     <!DO ...

  2. JS动画之缓动函数分析及动画库

    上一篇讲了JS动画定时器相关知识,这一篇介绍下缓动函数及流行的动画库. 熟悉的图 实际使用 jquery animate()+jquery.easing插件的使用: $(selector).anima ...

  3. NGUI缓动函数

    缓动函数:http://easings.net/zh-cn 研究NGUI的博客:http://dsqiu.iteye.com/category/295721

  4. iOS基本动画/关键帧动画/利用缓动函数实现物理动画效果

    先说下基本动画部分 基本动画部分比较简单, 但能实现的动画效果也很局限 使用方法大致为: #1. 创建原始UI或者画面 #2. 创建CABasicAnimation实例, 并设置keypart/dur ...

  5. Silverlight动画学习笔记(三):缓动函数

    (一)定义: 缓动函数:可以将自定义算术公式应用于动画 (二)为什么要用缓动函数: 您可能希望某一对象逼真地弹回或其行为像弹簧一样.您可以使用关键帧动画甚至 From/To/By 动画来大致模拟这些效 ...

  6. JS —— 轮播图中的缓动函数的封装

    轮播图的根本其实就是缓动函数的封装,如果说轮播图是一辆跑动的汽车,那么缓动函数就是它的发动机,今天本文章就带大家由简入繁,封装属于自己的缓动函数~~ 我们从需求的角度开始,首先给出一个简单需求: 1. ...

  7. EaseType 缓动函数

    EaseType(动画曲线) EaseType 缓动函数或者我习惯叫它动画曲线,在很多的软件或动画中都有涉及到,下面是摘取的一些资料: 缓函数图例 Tween效果 每一幅图像当鼠标移上去,会有路径效果 ...

  8. WPF中的动画——(四)缓动函数

    缓动函数可以通过一系列公式模拟一些物理效果,如实地弹跳或其行为如同在弹簧上一样.它们一般应用在From/To/By动画上,可以使得其动画更加平滑. var widthAnimation = new D ...

  9. Tween + 缓动函数

    Unity-Tween http://www.cnblogs.com/MrZivChu/p/UnityTween.html iTween: iTween大解构(一)之抛物线移动 http://blog ...

随机推荐

  1. hibernate基本配置show_sql、sql_format

    show_sql是否打印生成的sql语句: <property name="show_sql">false</property> sql_format,格式 ...

  2. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

  3. JS+Zero Clipboard swf复制到剪贴板 兼容浏览器(bind事件绑定函数)

    转自http://www.ipmtea.net/css_ie_firefox/201107/07_499.html 1.ZeroClipboard其实是国外的一个js类库,源码结构如: var Zer ...

  4. malloc/free 与 new/delete 区别

    malloc/free 是c++/c的标准库函数,new/delete 是c++的运算符.两者都可以申请动态内存和释放内存.对于非内部数据类型的对象而言,光用malloc/free是无法满足动态对象的 ...

  5. Digitale Logik

    1.Zahl System und Code System 1.1 Die Rechnung des Ergänzungscode 1.2 Manche häufig verwendet Code 1 ...

  6. 基础语言知识JAVA

    1. 总结: JAVA比较重要的博客: http://www.runoob.com/java/java-tutorial.html     (JAVA教程) http://blog.csdn.net/ ...

  7. 深入了解javascript的sort方法

    在javascript中,数组对象有一个有趣的方法 sort,它接收一个类型为函数的参数作为排序的依据.这意味着开发者只需要关注如何比较两个值的大小,而不用管“排序”这件事内部是如何实现的.不过了解一 ...

  8. css3 响应式布局 Media Query

    1.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简单说就是一个网站能够兼容多个终端. 2.响应式布局的优缺点? 优点:面对不同分辨率设备灵活性强,快捷 ...

  9. TextBox 控件

    TextBox控件上有一个箭头,MultiLine属性,是多行显示 TextBox控件有System.Windows.TextBox类提供,提供了基本的文本输入和编辑功能           属性 A ...

  10. MYSQL登录错误:mysqladmin: connect to server at ‘localhost’ failed

    一.mysql登录错误 mysqladmin: connect to server at 'localhost' failed       error: 'Access denied for user ...