速度采样器 (VelocitySampler)

一个小的 Swift 结构体,用于计算 2D 或 3D 速度的滚动平均值(通常由触摸驱动)。

安装

可以使用 Swift Package Manager 将 VelocitySampler 添加到你的项目中。 有关在 Xcode 中使用 SwiftPM 的更多信息,请参阅Apple 的指南

如果你直接使用包依赖项,你可以将此添加为你的依赖项之一

dependencies: [
    .package(url: "https://github.com/harlanhaskins/VelocitySampler.git", from: "0.0.1")
]

用法

VelocitySampler 是一个结构体,你可以逐步添加越来越多的样本。 它维护一个内部的 N 个样本的环形缓冲区,并持续跟踪和平均这些样本。

var sampler = VelocitySampler()

// While the user's moving a touch around, update the sampler

sampler.addSample(touch.location(in: view))
// ... repeat many times

// Extract the velocity
let velocity = sampler.velocity
print(velocity) // Do something with it

作者

Harlan Haskins (harlan@harlanhaskins.com)