将不断变化的标量值转换为变化的间隔或频率
尽管我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,要明白这是一个“测量”问题。为了解决这个问题,需要分离出解决问题所需的最小属性并导出其余属性。
移动的距离和移动该距离所花费的时间是每次测量的派生属性,在这种情况下,测量被命名为“滑动”。在赛车类比中,测量值称为一圈。
现在可以计算“速度”。这将是“速度”,即距离/时间。
可以根据滑动的起点和终点计算
距离
。要获取时间值,请在
touchesBegan:withEvents:
和touchesEnded:withEvents:
中创建NSDate
的startTime
实例> 使用[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 ofNSDate
intouchesBegan:withEvents:
and intouchesEnded:withEvents:
calculateelapsedTimeInterval
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.