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 ...
随机推荐
- ubuntu16.04 nginx安装
.gcc.g++依赖库 apt-get install build-essential apt-get install libtool .安装 pcre依赖库(http://www.pcre.org/ ...
- xgboost 参数
XGBoost 参数 在运行XGBoost程序之前,必须设置三种类型的参数:通用类型参数(general parameters).booster参数和学习任务参数(task parameters). ...
- Linux-(diff)
diff 命令 1.命令格式: diff [参数] [文件1或目录1] [文件2或目录2] 2.命令功能: diff命令能比较单个文件或者目录内容.如果指定比较的是文件,则只有当输入为文本文件时 ...
- 玩转mongodb(四):细说插入、更新、删除和查询
插入: 使用insert或save方法想目标集合插入一个文档: db.person.insert({"name":"ryan","age": ...
- 短视频APP是如何开启你的美好生活的?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯视频云终端团队发表于云+社区专栏 常青, 2008 年毕业加入腾讯,一直从事客户端研发相关工作,先后参与过 PC QQ.手机QQ. ...
- 布局管理器之CardLayout(卡片布局管理器)
对于选项卡这个概念大家可能不会陌生,就是在一个窗口中可以切换显示多页不同的内容,但同一时间只能是其中的某一页可见的,这样的一个个的页面就是选项卡. CardLayout就是类似的这样一个布局管理器,它 ...
- 使用Windbg找出死锁,解决生产环境中运行的软件不响应请求的问题
前言 本文介绍本人的一次使用Windbg分析dump文件找出死锁的过程,并重点介绍如何确定线程所等待的锁及判断是否出现了死锁. 对于如何安装及设置Windbg请参考:<使用Windbg和SoS扩 ...
- vue里面computed的运用理解
computed 计算属性,是用来声明式的描述一个值依赖了其它的值,当你在模板里把数据绑定到一个计算属性上时,Vue 会在其依赖的任何值导致该计算属性改变时更新 DOM.这个功能非常强大,它可以让你的 ...
- C#中const 和 readonly 修饰符的用法详解
1.const是不变常量,在编译的时候就需要有确定的值,只能用于数值和字符串,或者引用类型只能为null.(这里为什么要把字符串单独拿出来?是因为字符串string是引用类型,但是使用的时候却感觉是值 ...
- 一次查找Windows Live Writer的VSPaste插件丢失RTF格式信息的经历
背景 我在博客园上写博客是使用Windows Live Writer,代码高亮插件是使用Paste from Visual Studio(下文简称VSPaste). Windows Live Writ ...