一个用于 Cocoa 和 Cocoa Touch 的插值框架。
该框架定义了一个协议,LNInterpolable(在 Swift 中为 Interpolable),定义了一个通用的 API,用于在两个值之间进行插值。
该协议定义了两个方法。
- (instancetype)interpolateToValue:(id)toValue progress:(double)progress;
- (instancetype)interpolateToValue:(id)toValue progress:(double)progress behavior:(LNInterpolationBehavior)behavior;
public func interpolate(to toValue: Any, progress: Double) -> Self
public func interpolate(to toValue: Any, progress: Double, behavior: InterpolationBehavior) -> Self
第一个变体根据进度返回原始值和 toValue 之间的插值,使用默认行为。 第二个变体类似,但接收一个行为修饰符。 每个实现都可以使用它来选择如何实现值之间的插值。 默认值 LNInterpolationBehaviorUseDefault(在 Swift 中为 .useDefault)始终可用。
该框架为可能需要插值的最常见情况提供了实现。
为 NSValue 提供了实现,它为以下值提供支持
NSNumber)NSDecimalNumber)CGPoint, CGSize, CGVector, CGRect, CGAffineTransform (NSValue)UIOffset, UIEdgeInsets (NSValue)NSEdgeInsets (NSValue)为颜色(iOS 和 macOS 均适用)提供了实现。 默认情况下,颜色插值在 Lab 色彩空间中执行,以获得最佳可能的插值质量。 如果性能成为问题,您可以使用 LNInterpolationBehaviorUseRGBColorSpace(在 Swift 中为 .useRGBColorSpace)来指定使用 RGB 色彩空间的行为。
提供了一个演示 playground。 打开 LNInterpolationExample/LNInterpolationExample.xcworkspace 并为 macOS 构建框架。 打开 playground 并浏览可用的页面。
Swift Package Manager 是将 LNInterpolation 集成到您的项目中的推荐方式。
LNInterpolation 支持 SPM 5.1.0 及更高版本。 要使用 SPM,您应该使用 Xcode 11 打开您的项目。 点击 File -> Swift Packages -> Add Package Dependency,输入 https://github.com/LeoNatan/LNInterpolation。 选择您想要使用的版本。
您也可以手动将包添加到您的 Package.swift 文件中
.package(url: "https://github.com/LeoNatan/LNInterpolation.git", from: "1.0")
以及目标中的依赖项
.target(name: "BestExampleApp", dependencies: ["LNInterpolation"]),
将以下内容添加到您的 Cartfile
github "LeoNatan/LNInterpolation"
将 LNInterpolation.xcodeproj 项目拖到您的项目中,并将 LNInterpolation.framework 添加到您的项目目标的 General 选项卡中的 Embedded Binaries。 Xcode 应该会自动处理其他一切。