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#)
最近有个需求,需要将C# winform中的listBox中的内容自动换行, 其实在用listBox前,已经用textBox实现了大部分功能,可惜最后还是有个焦点的问题, 就是textBox中的文字会 ...
- 基于obs+nginx-rtmp-module搭建自己直播的系统
前言 一句唠叨,工欲善其事,必先利其器,在程序员的工作里,搭建各种环境往往花费过多不必要的时间,这里建议搭建服务端环境时,尽量避开win.macos这种系统,个人比较推崇centos. 操作 下面以c ...
- SSH 整合 (Maven)
1.SSH 教程详见我的上一篇博客 SSH(Struts 2.3.31 + Spring 4.1.6 + Hibernate 5.0.12 + Ajax)框架整合实现简单的增删改查(包含分页,Ajax ...
- [codeup] 1128 出租车费
题目描述 某市出租车计价规则如下:起步4公里10元,即使你的行程没超过4公里:接下来的4公里,每公里2元:之后每公里2.4元.行程的最后一段即使不到1公里,也当作1公里计费. 一个乘客可以根据行程公里 ...
- i.mx6 Android5.1.1 build解析
参考资料:理解 Android Build 系统 把总结放前面: 1. 常用编译命令 make clean 执行清理,等同于:rm -rf out/. make sdk 编译出 Android 的 S ...
- 三种数据库访问——Spring JDBC
本篇随笔是上两篇的延续:三种数据库访问——原生JDBC:数据库连接池:Druid Spring的JDBC框架 Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要 ...
- 消息管理-activemq
1.ConnectionFactory("tcp://192.168.1.103:61616/"); 本机地址192.168.1.103 也 可以是localhost 消息的生产者 ...
- SQL Server操作结果集-并集 差集 交集 结果集排序
操作结果集 为了配合测试,特地建了两个表,并且添加了一些测试数据,其中重复记录为东吴的人物. 表:Person_1魏国人物 表:Person_2蜀国人物 A.Union形成并集 Union可以对两个或 ...
- attempt to write a readonly database错误的解决(C#,SQLite)
今天打包WPF程序,安装后总是打不开,查看监控日志原来是SQLite的问题,报错如图 当向SQLite数据库中存入新纪录时总是显示attempt to write a readonly a datab ...
- springboot之定时任务@Scheduled
1.pom.xml中导入必要的依赖: <parent> <groupId>org.springframework.boot</groupId> <artifa ...