Swift-EasingAnimation
Swift-EasingAnimation

效果


源码
https://github.com/YouXianMing/UI-Component-Collection
//
// Easing.swift
// Swift-EasingAnimation
//
// Created by YouXianMing on 15/10/21.
//
// https://github.com/YouXianMing
// http://home.cnblogs.com/u/YouXianMing/
// import UIKit enum EasingFunction: Int { case
LinearInterpolation = , // Quadratic easing; p^2
QuadraticEaseIn,
QuadraticEaseOut,
QuadraticEaseInOut, // Cubic easing; p^3
CubicEaseIn,
CubicEaseOut,
CubicEaseInOut, // Quartic easing; p^4
QuarticEaseIn,
QuarticEaseOut,
QuarticEaseInOut, // Quintic easing; p^5
QuinticEaseIn,
QuinticEaseOut,
QuinticEaseInOut, // Sine wave easing; sin(p * PI/2)
SineEaseIn,
SineEaseOut,
SineEaseInOut, // Circular easing; sqrt(1 - p^2)
CircularEaseIn,
CircularEaseOut,
CircularEaseInOut, // Exponential easing, base 2
ExponentialEaseIn,
ExponentialEaseOut,
ExponentialEaseInOut, // Exponentially-damped sine wave easing
ElasticEaseIn,
ElasticEaseOut,
ElasticEaseInOut, // Overshooting cubic easing;
BackEaseIn,
BackEaseOut,
BackEaseInOut, // Exponentially-decaying bounce easing
BounceEaseIn,
BounceEaseOut,
BounceEaseInOut func value() -> ((Double) -> Double) { switch self { case .LinearInterpolation:
return Easing.LinearInterpolation case .QuadraticEaseIn:
return Easing.QuadraticEaseIn case .QuadraticEaseOut:
return Easing.QuadraticEaseOut case .QuadraticEaseInOut:
return Easing.QuadraticEaseInOut case .CubicEaseIn:
return Easing.CubicEaseIn case .CubicEaseOut:
return Easing.CubicEaseOut case .CubicEaseInOut:
return Easing.CubicEaseInOut case .QuarticEaseIn:
return Easing.QuarticEaseIn case .QuarticEaseOut:
return Easing.QuarticEaseOut case .QuarticEaseInOut:
return Easing.QuarticEaseInOut case .QuinticEaseIn:
return Easing.QuinticEaseIn case .QuinticEaseOut:
return Easing.QuinticEaseOut case .QuinticEaseInOut:
return Easing.QuinticEaseInOut case .SineEaseIn:
return Easing.SineEaseIn case .SineEaseOut:
return Easing.SineEaseOut case .SineEaseInOut:
return Easing.SineEaseInOut case .CircularEaseIn:
return Easing.CircularEaseIn case .CircularEaseOut:
return Easing.CircularEaseOut case .CircularEaseInOut:
return Easing.CircularEaseInOut case .ExponentialEaseIn:
return Easing.ExponentialEaseIn case .ExponentialEaseOut:
return Easing.ExponentialEaseOut case .ExponentialEaseInOut:
return Easing.ExponentialEaseInOut case .ElasticEaseIn:
return Easing.ElasticEaseIn case .ElasticEaseOut:
return Easing.ElasticEaseOut case .ElasticEaseInOut:
return Easing.ElasticEaseInOut case .BackEaseIn:
return Easing.BackEaseIn case .BackEaseOut:
return Easing.BackEaseOut case .BackEaseInOut:
return Easing.BackEaseInOut case .BounceEaseIn:
return Easing.BounceEaseIn case .BounceEaseOut:
return Easing.BounceEaseOut case .BounceEaseInOut:
return Easing.BounceEaseInOut
}
}
} class Easing: NSObject { // MARK: Linear interpolation (no easing)
class func LinearInterpolation(p : Double) -> Double { return p
} // MARK: Quadratic easing; p^2
class func QuadraticEaseIn(p : Double) -> Double { return p * p
} class func QuadraticEaseOut(p : Double) -> Double { return -(p * (p - ))
} class func QuadraticEaseInOut(p : Double) -> Double { if (p < 0.5) { return * p * p } else { return (- * p * p) + ( * p) -
}
} // MARK: Cubic easing; p^3
class func CubicEaseIn(p : Double) -> Double { return p * p * p
} class func CubicEaseOut(p : Double) -> Double { let f : Double = (p - )
return f * f * f +
} class func CubicEaseInOut(p : Double) -> Double { if (p < 0.5) { return * p * p * p } else { let f : Double = (( * p) - )
return 0.5 * f * f * f +
}
} // MARK: Quartic easing; p^4
class func QuarticEaseIn(p : Double) -> Double { return p * p * p * p
} class func QuarticEaseOut(p : Double) -> Double { let f : Double = (p - )
return f * f * f * ( - p) +
} class func QuarticEaseInOut(p : Double) -> Double { if(p < 0.5) { return * p * p * p * p } else { let f : Double = (p - );
return - * f * f * f * f +
}
} // MARK: Quintic easing; p^5
class func QuinticEaseIn(p : Double) -> Double { return p * p * p * p * p
} class func QuinticEaseOut(p : Double) -> Double { let f : Double = (p - )
return f * f * f * f * f +
} class func QuinticEaseInOut(p : Double) -> Double { if (p < 0.5) { return * p * p * p * p * p } else { let f : Double = (( * p) - )
return 0.5 * f * f * f * f * f +
}
} // MARK: Sine wave easing; sin(p * PI/2)
class func SineEaseIn(p : Double) -> Double { return sin((p - ) * M_PI_2) +
} class func SineEaseOut(p : Double) -> Double { return sin(p * M_PI_2)
} class func SineEaseInOut(p : Double) -> Double { return 0.5 * ( - cos(p * M_PI))
} // MARK: Circular easing; sqrt(1 - p^2)
class func CircularEaseIn(p : Double) -> Double { return - sqrt( - (p * p))
} class func CircularEaseOut(p : Double) -> Double { return sqrt(( - p) * p)
} class func CircularEaseInOut(p : Double) -> Double { if (p < 0.5) { return 0.5 * ( - sqrt( - * (p * p))) } else { return 0.5 * (sqrt(-(( * p) - ) * (( * p) - )) + )
}
} // MARK: Exponential easing, base 2
class func ExponentialEaseIn(p : Double) -> Double { return (p == 0.0) ? p : pow(, * (p - ))
} class func ExponentialEaseOut(p : Double) -> Double { return (p == 1.0) ? p : - pow(, - * p)
} class func ExponentialEaseInOut(p : Double) -> Double { if (p == 0.0 || p == 1.0) { return p
} if (p < 0.5) { return 0.5 * pow(, ( * p) - ) } else { return -0.5 * pow(, (- * p) + ) +
}
} // MARK: Exponentially-damped sine wave easing
class func ElasticEaseIn(p : Double) -> Double { return sin( * M_PI_2 * p) * pow(, * (p - ))
} class func ElasticEaseOut(p : Double) -> Double { return sin(- * M_PI_2 * (p + )) * pow(, - * p) +
} class func ElasticEaseInOut(p : Double) -> Double { 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 - )) + )
}
} // MARK: Overshooting cubic easing
class func BackEaseIn(p : Double) -> Double { return p * p * p - p * sin(p * M_PI)
} class func BackEaseOut(p : Double) -> Double { let f : Double = ( - p);
return - (f * f * f - f * sin(f * M_PI))
} class func BackEaseInOut(p : Double) -> Double { if (p < 0.5) { let f : Double = * p
return 0.5 * (f * f * f - f * sin(f * M_PI)) } else { let f : Double = ( - (*p - ))
let tmp : Double = (f * f * f - f * sin(f * M_PI))
return 0.5 * ( - tmp) + 0.5
}
} // MARK: Exponentially-decaying bounce easing
class func BounceEaseIn(p : Double) -> Double { return - BounceEaseOut( - p)
} class func BounceEaseOut(p : Double) -> Double { 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
}
} class func BounceEaseInOut(p : Double) -> Double { if (p < 0.5) { return 0.5 * BounceEaseIn(p*) } else { return 0.5 * BounceEaseOut(p * - ) + 0.5
}
}
}
//
// EasingValue.swift
// Swift-EasingAnimation
//
// Created by YouXianMing on 15/10/21.
//
// https://github.com/YouXianMing
// http://home.cnblogs.com/u/YouXianMing/
// import UIKit class EasingValue: NSObject { // MARK: var /// 动画函数
var function : EasingFunction! /// 关键帧点数
var frameCount : size_t! // MARK: init
override init() { super.init() function = EasingFunction.SineEaseIn
frameCount =
} init(withFunction : EasingFunction, frameCount : size_t) { super.init() self.function = withFunction
self.frameCount = frameCount
} // MARK: func /**
计算关键帧 - parameter fromValue: 起始值
- parameter toValue: 结束值 - returns: 关键帧值数组
*/
func frameValueWith(fromValue fromValue : Double, toValue : Double) -> [AnyObject] { let values = NSMutableArray(capacity: frameCount) var t : Double = 0.0
let dt : Double = 1.0 / (Double(frameCount) - ) for var i = ; i < frameCount; ++i, t += dt { let value = fromValue + (function.value())(t) * (toValue - fromValue)
values.addObject(value)
} return values as [AnyObject]
} /**
计算关键帧点 - parameter fromPoint: 起始点
- parameter toPoint: 结束点 - returns: 关键帧点数组
*/
func pointValueWith(fromPoint fromPoint : CGPoint, toPoint : CGPoint) -> [AnyObject] { let values = NSMutableArray(capacity: frameCount) var t : Double = 0.0
let dt : Double = 1.0 / (Double(frameCount) - ) for var i = ; i < frameCount; ++i, t += dt { let x : Double = Double(fromPoint.x) + (function.value())(t) * (Double(toPoint.x) - Double(fromPoint.x))
let y : Double = Double(fromPoint.y) + (function.value())(t) * (Double(toPoint.y) - Double(fromPoint.y))
let point : CGPoint = CGPoint(x : x, y : y)
values.addObject(NSValue(CGPoint: point))
} return values as [AnyObject]
} /**
计算关键帧尺寸 - parameter fromSize: 起始尺寸
- parameter toSize: 结束尺寸 - returns: 关键帧尺寸数组
*/
func sizeValueWith(fromSize fromSize : CGSize, toSize : CGSize) -> [AnyObject] { let values = NSMutableArray(capacity: frameCount) var t : Double = 0.0
let dt : Double = 1.0 / (Double(frameCount) - ) for var i = ; i < frameCount; ++i, t += dt { let width : Double = Double(fromSize.width) + (function.value())(t) * (Double(toSize.width) - Double(fromSize.width))
let height : Double = Double(fromSize.height) + (function.value())(t) * (Double(toSize.height) - Double(fromSize.height))
let size : CGSize = CGSize(width: width, height: height)
values.addObject(NSValue(CGSize : size))
} return values as [AnyObject]
}
}
//
// ComplexEasingValue.swift
// Swift-EasingAnimation
//
// Created by YouXianMing on 15/10/21.
//
// https://github.com/YouXianMing
// http://home.cnblogs.com/u/YouXianMing/
// import UIKit class ComplexEasingValue: EasingValue { /// 点A的动画函数(如果是 size,则点 A 表示 width;如果是 point,则点 A 表示 x)
var functionA : EasingFunction! /// 点B的动画函数(如果是 size,则点 B 表示 height;如果是 point,则点 B 表示 y)
var functionB : EasingFunction! // MARK: init
override init() { super.init() functionA = EasingFunction.SineEaseIn
functionB = EasingFunction.SineEaseIn
frameCount =
} init(withFunctionA : EasingFunction, FunctionB : EasingFunction, frameCount : size_t) { super.init() functionA = withFunctionA
functionB = FunctionB
self.frameCount = frameCount
} /**
计算关键帧 - parameter fromValue: 起始值
- parameter toValue: 结束值 - returns: 关键帧值数组
*/
override func pointValueWith(fromPoint fromPoint : CGPoint, toPoint : CGPoint) -> [AnyObject] { let values = NSMutableArray(capacity: frameCount) var t : Double = 0.0
let dt : Double = 1.0 / (Double(frameCount) - ) for var i = ; i < frameCount; ++i, t += dt { let x : Double = Double(fromPoint.x) + (functionA.value())(t) * (Double(toPoint.x) - Double(fromPoint.x))
let y : Double = Double(fromPoint.y) + (functionB.value())(t) * (Double(toPoint.y) - Double(fromPoint.y))
let point : CGPoint = CGPoint(x : x, y : y)
values.addObject(NSValue(CGPoint: point))
} return values as [AnyObject]
} /**
计算关键帧点 - parameter fromPoint: 起始点
- parameter toPoint: 结束点 - returns: 关键帧点数组
*/
override func sizeValueWith(fromSize fromSize : CGSize, toSize : CGSize) -> [AnyObject] { let values = NSMutableArray(capacity: frameCount) var t : Double = 0.0
let dt : Double = 1.0 / (Double(frameCount) - ) for var i = ; i < frameCount; ++i, t += dt { let width : Double = Double(fromSize.width) + (functionA.value())(t) * (Double(toSize.width) - Double(fromSize.width))
let height : Double = Double(fromSize.height) + (functionB.value())(t) * (Double(toSize.height) - Double(fromSize.height))
let size : CGSize = CGSize(width: width, height: height)
values.addObject(NSValue(CGSize : size))
} return values as [AnyObject]
}
}
细节

Swift-EasingAnimation的更多相关文章
- Swift - EasingAnimation绘制圆环动画
Swift - EasingAnimation绘制圆环动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CircleView ...
- iOS代码规范(OC和Swift)
下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...
- Swift与C#的基础语法比较
背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...
- iOS开发系列--Swift语言
概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...
- 算法与数据结构(十七) 基数排序(Swift 3.0版)
前面几篇博客我们已经陆陆续续的为大家介绍了7种排序方式,今天博客的主题依然与排序算法相关.今天这篇博客就来聊聊基数排序,基数排序算法是不稳定的排序算法,在排序数字较小的情况下,基数排序算法的效率还是比 ...
- 算法与数据结构(十五) 归并排序(Swift 3.0版)
上篇博客我们主要聊了堆排序的相关内容,本篇博客,我们就来聊一下归并排序的相关内容.归并排序主要用了分治法的思想,在归并排序中,将我们需要排序的数组进行拆分,将其拆分的足够小.当拆分的数组中只有一个元素 ...
- Swift enum(枚举)使用范例
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- swift开发新项目总结
新项目用swift3.0开发,现在基本一个月,来总结一下遇到的问题及解决方案 1,在确定新项目用swift后,第一个考虑的问题是用纯swift呢?还是用swift跟OC混编 考虑到新项目 ...
- swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明
关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明 open,p ...
- 【swift】BlockOperation和GCD实用代码块
//BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...
随机推荐
- C#循环读取文件流,按行读取
public Dictionary<string,string> GetSourceDisksElements(String section) { section = "[&qu ...
- 简单canvas刮刮乐
好久没玩canvas了,随便写点效果玩玩.要开始重新拾起这门牛x的技术了,工作中不一定能用得上,以后说不定就能发挥用处了. <!DOCTYPE html> <html lang=&q ...
- [Linux]——进程管理相关
一些概念 程序program:通常以二进制程序放置在存储媒介中,以物理文件形式存在 进程process:程序通过用户执行被触发后,执行者的权限与属性.程序的代码和所需数据会被加载到内存中,OS给予这个 ...
- Mysql备份工具xtraback全量和增量测试
Mysql备份工具xtraback全量和增量测试 xtrabackup 是 percona 的一个开源项目,可以热备份innodb ,XtraDB,和MyISAM(会锁表) 官方网址http:// ...
- [转]Gridview实现多列排序,并显示图标
本文转自:http://blog.csdn.net/gmjinrong/article/details/4516301 GridView实现支持多列排序,并显示升.降序图标上网找了很多资料参考才解决了 ...
- mvc中的action验证登录(ActionFilterAttribute)
方法一 : 1.创建一个全局action过滤器 (在appstart 的filterconfig中注册 filters.Add(new LoginAttribute());) 2.不需要登 ...
- springboot之约定大约配置
前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样 ...
- SQL存储过程基础语法及实例
1.定义变量简单赋值 declare @a int //声明一个变量a 赋初值为5 print @a //输出变量a 2.创建临时表 if OBJECT_ID('tempdb.#FlightState ...
- Microservices与DDD的关系
Microservices(微服务架构)和DDD(领域驱动设计)是时下最炙手可热的两个技术词汇.在最近两年的咨询工作中总是会被不同的团队和角色询问,由此也促使我思考为什么这两个技术词汇被这么深入人心的 ...
- 现代 C++ 编译时 结构体字段反射
基于 C++ 14 原生语法,不到 100 行代码:让编译器帮你写 JSON 序列化/反序列化代码,告别体力劳动.