将不断变化的标量值转换为变化的间隔或频率

发布于 2024-09-05 14:33:38 字数 327 浏览 3 评论 0原文

尽管我使用 Objective C 进行编码,但这更多的是一个一般性的编程问题。
将不断变化的标量值转换为变化的间隔或频率的最佳方法是什么?

现在,每次标量值发生变化时,我都会破坏 NSInterval

   [self.myTimer invalidate];
    self.myTimer = nil;

并创建一个新的,但这似乎是实现我的目标的一种非常昂贵的方式,因为在我的情况下,变化的标量值代表滑动的水平速度。

粗略地类比一下,想象一下视觉节拍器中反映的滑动速度,滑动越快,节拍器的频率就越高(间隔越短)。

Although I'm coding in Objective C, this is more of a general programming question.

What is the best way to convert a constantly changing scalar value to a changing interval or frequency?

Right now every time the scalar value changes I am destroying the NSInterval
ie

   [self.myTimer invalidate];
    self.myTimer = nil;

and creating a new one, but this seems like a VERY expensive way to achieve my goal, since the changing scalar value in my case represents the horizontal velocity of a swipe.

For a rough analogy, think of the speed of a swipe being reflected in a visual metronome, the faster you swipe, the higher(shorter interval) the frequency of the metronome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-09-12 14:33:38

首先,要明白这是一个“测量”问题。为了解决这个问题,需要分离出解决问题所需的最小属性并导出其余属性。

移动的距离和移动该距离所花费的时间是每次测量的派生属性,在这种情况下,测量被命名为“滑动”。在赛车类比中,测量值称为一圈。
现在可以计算“速度”。这将是“速度”,即距离/时间。

可以根据滑动的起点和终点计算距离
要获取时间值,请在 touchesBegan:withEvents:touchesEnded:withEvents: 中创建 NSDatestartTime 实例> 使用 [startTime timeIntervalSinceNow] 计算 elapsedTimeInterval;

根据您的需要,您可能需要一个 Measurement 类,其中包含 startPosition、endPosition、startTime 和endTime,以便您可以跟踪“最快”速度等。

请查看分析模式< /a> 作者:马丁·福勒。我发现当尝试将领域问题映射到软件解决方案时它非常有用。

First, understand that this is a "measurement" problem. To solve this problem, isolate the minimum attributes needed to solve the problem and derive the rest.

The distance moved and the time taken to move the distance are derived attributes of each measurement, in this case the measurement is named "swipe". In a racing analogy, the measurement is called a lap.
The "speed" can now be calculated. This will be the "velocity", which is simply distance/time.

The distance can be calculated given the start and end points of the swipe.
To obtain the time value, create an startTime instance of NSDate in touchesBegan:withEvents: and in touchesEnded:withEvents: calculate elapsedTimeInterval using [startTime timeIntervalSinceNow];

Depending on your needs, you may need a Measurement class with properties for startPosition, endPosition, startTime and endTime so you can keep track of "fastest" speed etc.

Take a look at Analysis Patterns by Martin Fowler. I find it very useful when trying to map domain problems to software solutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文