简体   繁体   English

如何找到在2点之间移动UIView的剩余距离

[英]How To Find Remaining Distance To Move UIView Between 2 Points

SCENARIO 场景

I am working on an application that keeps track of blood glucose levels into a graph. 我正在开发一个将血糖水平跟踪到图表中的应用程序。 On the graph there are "markings" (ex: -200mg) going in vertical order along the y axis on the right side of the screen and "hours" (ex: -12:00 PM) will be along the x axis on the bottom of the graph. 在图表上,屏幕右侧的y轴沿垂直方向排列有“标记”(例如:-200mg),而“小时数”(例如:-12:00 PM)将沿屏幕的x轴显示。图的底部。 I have to plot out little 'dots' to display what the blood glucose level was throughout the way. 我必须画出一点“点”来显示整个过程中的血糖水平。

ISSUE 问题

I am trying to calculate how to position the 'dots' in the correct time and mg level and I'm having difficulty calculating the positions. 我正在尝试计算如何在正确的时间和毫克水平上定位“点”,而我在计算位置时遇到困难。 I can access the "markings" and retrieve it's marking.center.x to indicate which 'Time Slot' (x axis) and the marking.center.y to indicate which 'MG Level' the 'dot' needs to go into. 我可以访问“标记”并检索它的marking.center.x以指示哪个“时间槽”(x轴),以及marking.center.y指示该“点”需要进入哪个“ MG等级”。 Problem is it isn't always exactly 12:00 PM or 200mg where it will need to be placed. 问题是,并非总是将其放置在12:00 PM或200mg处。 In fact that would be very rare. 实际上那将是非常罕见的。

WHAT I NEED 我需要的


Based on the following variables: 基于以下变量:

dot.mgLevel The dot will already know where it needs to go based on the information retrieved from the medical device. dot.mgLevel根据从医疗设备检索到的信息,点将已经知道需要去哪里。 It will know the time and mgLevel to assign itself. 它会知道分配时间和mgLevel。

marking.mgLevel The markings will each have evenly distributed values that such as -100mg , -200mg , -300mg ect... marking.mgLevel标记的每个值均具有均匀分布的值,例如-100mg-200mg-300mg

timemarking.timeslot Each time marking on the bottom will each have evenly distributed times allocated every 30 min. timemarking.timeslot底部的每个时间标记每30分钟分配一次均匀分布的时间。 Such as -12:00PM , -12:30PM , -1:00PM ect... 例如-12:00PM -12:30PM -1:00PM ...


If the dot has a mg Level of 330mg and the closest marking on the mg Level is 300mg, then I need to be able to calculate how much further up the dot needs to move from 300 going towards the 400mg marker. 如果该点的毫克水平为330mg,并且毫克水平上最接近的标记为300mg,那么我需要能够计算出该点从300向上移动到400mg标记所需的距离。

SO... 所以...

If the distance between the markings are 100pt and the dot's mgLevel is 330mg, then I know that I need to move the dot from the 300mg marking toward the 400mg marking by exactly 30pt. 如果标记之间的距离为100pt,并且点的mgLevel为330mg,那么我知道我需要将点从300mg标记向400mg标记精确地移动30pt。 That's because it's simple math because the distance between the markings is 100. But in real life it isn't 100, so I need to be able to calculate this. 那是因为这是简单的数学运算,因为标记之间的距离是100。但是在现实生活中,它不是100,因此我需要能够计算出来。

MY ULTIMATE QUESTION 我的最终问题

Say distance between markings is 241 and each marking represents multiples of a hundred. 说标记之间的距离是241,每个标记代表一百的倍数。 Say my dot has a mgLevel of 412. How do I calculate how far I need to move the dot so that it will be in the correct place? 假设我的点的mgLevel为412。如何计算将点移动到正确位置所需的距离?

I THINK? 我认为?

I think I need to make 241 equal 100%. 我想我需要使241等于100%。 But I need help. 但是我需要帮助。

Distance between markings is 241pt Markings are multiples of 100mg 1mg will occupy 2.41pt. 标记之间的距离为241pt。标记是100mg的倍数,1mg将占据2.41pt。 So 412mg will occupy (2.41 * 412) pt. 因此412mg将占据(2.41 * 412)点。 To know how much to move for the next dot, take the difference in mg and multiply by 2.41. 要知道下一个点要移动多少,将其差值mg乘以2.41。 In general, if distance between 2 markings in points is d , markings are multiples of m , and desired accuracy is k decimal places, 1mg will occupy g : let divisor = pow(10.0, Double(k)) let g = round((d/m)*divisor) / divisor 通常,如果两个标记点之间的距离是d ,标记是m的倍数,并且期望的精度是k个小数位,则1mg将占据g :let divisor = pow(10.0,Double(k))let g = round(( d / m)*除数)/除数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM