Popsicle是一个Swift框架,用于创建和管理不同值类型的插值,并内置UIKit支持。
将Popsicle文件夹中的所有文件拖动并复制到您的项目中。
首先,您需要一个Interpolator
实例
let interpolator = Interpolator()
下一步,您需要将一些Interpolation<T>
实例添加到您的插值器中。以下示例中,我们将在时间为0到150之间插值一个UIView的alpha值
let interpolation = Interpolation(yourView, alpha)
interpolation[0] = 0
interpolation[150] = 1
self.interpolator.addInterpolation(interpolation)
注意,alpha
是一个内置的KeyPath<T, U>
常量。Popsicle提供了一组UIKit相关KeyPaths,可以直接使用。您也可以使用一个完全自定义的关键路径。
您还可以修改在给定时间点使用的缓动函数
interpolation.setEasingFunction(EasingFunctionEaseOutQuad, forTime: 0)
有几个内置缓动函数可供选择。
最后,只需根据需要使interpolator
的time
变化。例如,一个UITableView
的内容偏移量
func scrollViewDidScroll(scrollView: UIScrollView) {
interpolator.time = Double(scrollView.contentOffset.y)
}
您可以通过让 类型符合Interpolable
协议来声明一个可插值的值类型。
以下是一个例子,看看CGPoint
是如何符合Interpolable
的
extension CGSize: Interpolable {
public static func interpolate(from fromValue: CGSize, to toValue: CGSize, withProgress progress: Progress) -> CGSize {
let width = CGFloat.interpolate(from: fromValue.width, to: toValue.width, withProgress: progress)
let height = CGFloat.interpolate(from: fromValue.height, to: toValue.height, withProgress: progress)
return CGSizeMake(width, height)
}
public static func objectify(value: CGSize) -> AnyObject {
return NSValue(CGSize: value)
}
}
Popsicle在MIT许可证下可用。