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 ...
随机推荐
- Linux时间命令
Linux一般有系统时间和硬件时间之分,date命令是显示和操作系统时间:hwclock用来操作硬件时间(日期).日期和时间很重要,比如错误的日期和时间会导致你不能编译程序. 1 date 用法: ...
- Oracle 12c 建表空间语句
create tablespace TBS_DYS datafile 'D:/Oracle_12c/app/dingyingsi/oradata/dingyingsi/TBS_DYS.ba' size ...
- 【设计模式】观察者模式 Observer Pattern
定义:观察者模式定义了对象之间的一对多依赖.当“主题”(Object)状态改变事,所有依赖它的“观察者”(Observer)都会受到通知并自动更新.主题支持观察者订阅和退订. 观察者模式提供了一种对象 ...
- SQL 修改表字段失败 解决方法
OK 大功告成 !!!
- asp后台读id设置样式
加runat=“server”
- solr+tomcat整合
一.solr安装 http://archive.apache.org/dist/lucene/solr/ 这个地址有各个版本的 这次我使用的是5.5.4版本和tomcat8 版本5.5.4已经内置了j ...
- Mysql explain分析sql语句执行效率
mysql优化–explain分析sql语句执行效率 Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 ...
- MarkDownPad 专业汉化破解
解压Pa_ttrar 运行Pa_ttrar.exe 点击下边第一个按钮“patch”——>弹出窗选择“YES” 选择“YES”后会选择一个文件,找到“C:\Users\用户名\AppD ...
- grafana 安装- 曲线图展示每秒新增数据量
下载: https://dl.grafana.com/oss/release/grafana-5.4.2.windows-amd64.zip 解压就能用 添加数据源 添加查询条件 sql 模式编写查询 ...
- Vue 2.0 pagination分页组件
最近写了一个分页组件,效果如下图: f-pagination.js文件 Vue.component('f-pagination',{ template:'<div class="fPa ...