Swift-EasingAnimation

效果

http://gizma.com/easing/

源码

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的更多相关文章

  1. Swift - EasingAnimation绘制圆环动画

    Swift - EasingAnimation绘制圆环动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CircleView ...

  2. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

  3. Swift与C#的基础语法比较

    背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...

  4. iOS开发系列--Swift语言

    概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...

  5. 算法与数据结构(十七) 基数排序(Swift 3.0版)

    前面几篇博客我们已经陆陆续续的为大家介绍了7种排序方式,今天博客的主题依然与排序算法相关.今天这篇博客就来聊聊基数排序,基数排序算法是不稳定的排序算法,在排序数字较小的情况下,基数排序算法的效率还是比 ...

  6. 算法与数据结构(十五) 归并排序(Swift 3.0版)

    上篇博客我们主要聊了堆排序的相关内容,本篇博客,我们就来聊一下归并排序的相关内容.归并排序主要用了分治法的思想,在归并排序中,将我们需要排序的数组进行拆分,将其拆分的足够小.当拆分的数组中只有一个元素 ...

  7. Swift enum(枚举)使用范例

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  8. swift开发新项目总结

    新项目用swift3.0开发,现在基本一个月,来总结一下遇到的问题及解决方案   1,在确定新项目用swift后,第一个考虑的问题是用纯swift呢?还是用swift跟OC混编      考虑到新项目 ...

  9. swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明

    关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明 open,p ...

  10. 【swift】BlockOperation和GCD实用代码块

    //BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...

随机推荐

  1. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(一):Kitty 系统介绍

    在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin 温馨提示: 有在演示环境删除数据的童鞋们,如果可以的话,麻烦动动小指,右键头像 ...

  2. xgboost 参数

    XGBoost 参数 在运行XGBoost程序之前,必须设置三种类型的参数:通用类型参数(general parameters).booster参数和学习任务参数(task parameters). ...

  3. springcloud-06-feign的使用

    在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Ap ...

  4. C#读取excel文件的内容(使用DataSet)

    C#读取Excel文件的内容,通过OLEDB来连接,关键是连接的路径,如:string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data S ...

  5. MBIST:用于嵌入式存储器的可测试设计技术

    MBist技术可以自动实现存储器单元或阵列的RTL级内建自测试电路,MBIST的EDA工具支持多种测试算法的自动实现,可针对一个或多个内嵌存储器自动创建BIST逻辑,并完成BIST逻辑与存储器的连接, ...

  6. Python描述符(__get__,__set__,__delete__)简介

    先说定义,这里直接翻译官方英文文档: 一般来说,描述符是具有“绑定行为”的对象属性,该对象的属性访问将会被描述符协议中的方法覆盖.这些方法是__get__(),__set__(),和__delete_ ...

  7. IOS第三方之MBProgressHUD

    // // ViewController.m // MBProgressHUD // // Created by City--Online on 15/6/15. // Copyright (c) 2 ...

  8. (在数据库中调用webservices。)SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问

    --开启 Ole Automation Procedures sp_configure 'show advanced options', 1; GO RECONFIGURE; GO sp_config ...

  9. java 数据脱敏

    所谓数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护.在涉及客户安全数据或者一些商业性敏感数据的情况下,在不违反系统规则条件下,对真实数据进行改造并提供测试使用,如身份 ...

  10. vue项目引入element

    前提工作-安装并配置好以下环境: 1.安装node  2.安装git 1.初始化项目 vue init webpack vue-elementui npm run dev 2.安装element np ...