如何使UISlider输出以指数方式输出漂亮的舍入数字? [英] How to make UISlider output nice rounded numbers exponentially?

查看:165
本文介绍了如何使UISlider输出以指数方式输出漂亮的舍入数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个用户可以操作设置距离的UISlider。
我从未使用过CocoaTouch UISlider,但是使用过其他框架滑块,通常有一个变量用于设置step和其他helper属性。



UISlider的文档只处理最大值和最小值,输出总是一个6位十进制浮点数,与滑块nob的位置成线性关系。我想我将不得不逐步实现所需的功能。



对于用户来说,最小/最大值范围从10米到999公里,我正在尝试以指数方式实现这一点,这对用户来说是自然的。即用户体验到对价值的控制感,无论大小。此外,输出具有合理的值。值10m 200m 2.5km 150 km等而不是1.2342356 m或108.93837756 km。



我希望前200米的步长增加10米,然后可能是50米到500米,然后当通过1000米的值时,它开始处理公里数,所以它的步长= 1公里直到50公里,然后可能是25公里的步数等。



无论如何,我最终会进行大量的舍入,并且每次用户移动滑块时都会在
a forrest语句和NSString / Number转换中包含大量计算只是一点点。



我希望有人可以借给我一些灵感/数学帮助,或者让我知道
a更精益的方法来解决这个问题。



我的最后一个想法是使用100个字符串值填充和数组,然后让滑块int值对应一个字符串,这不是很灵活,但可行。



提前感谢您提供的任何帮助:)

解决方案

最简单的答案是使用具有不同步长的分段控件。根据用户选择的选项,滑块将具有哪个步长。我甚至建议这可能是一种更友好的方式来接近它:)。



随意使用 Dapp 玩你的应用程序外观,看看分段控件如何适合设计。



然而......你想要更精简的方法;)。



我开始编写需要的10个左右的步骤,但我停止了意识到你可能已经找到了类似的解决方案。您的字符串数组的想法很好,我假设您只是将滑块值转换为整数并从数组中获取相关索引?



有时作为程序员我们走得太远用我们解决问题的方法。是的,字符串数组不是最灵活的解决方案,但速度很快!而且,我认为即使是天才的数学解决方案也不像你想象的那么灵活。此外,如果您不打算很快改变值,并且不需要在不同的滑块上使用不同的滑块范围,那么创建一个静态数组是有意义的。



<祝你好运! :)


I am implementing a UISlider a user can manipulate to set a distance. I have never used the CocoaTouch UISlider, but have used other frameworks sliders, usually there is a variable for setting the "step" and other "helper" properties.

The documentation for the UISlider deals only with a max and min value, and the output is always a 6 decimal float with a linear relation to the position of the "slider nob". I guess I will have to implement the desired functionality step by step.

To the user, the min/max values range from 10 m to 999 Km, I am trying to implement this in an exponential way, that will feel natural to the user. I.e. the user experiences a feeling of control over the values, big or small. Also that the "output" has reasonable values. Values like 10m 200m 2.5km 150 km etc. instead of 1.2342356 m or 108.93837756 km.

I would like for the step size to increase by 10m for the first 200m, then maybe by 50m up to 500m, then when passing the 1000 m value, it starts to deal with Kilometers, so then it is step size = 1 km up until 50 km, then maybe 25 km steps etc.

Any way I go about this I end up doing a lot of rounding and a lot of calculations wrapped in a forrest of if statements and NSString/Number conversions, each time the user moves the slider just a little.

I was hoping someone could lend me a bit of inspiration/math help or make me aware of a more lean approach to solving this problem.

My last idea is to populate and array with a 100 string values, then have the slider int value correspond to a string, this is not very flexible, but doable.

Thank you in advance for any help given:)

解决方案

The easiest answer is to use a Segmented Control with the different 'step sizes'. Depending on what option the user selects, is what step size your slider will have. I'd even recommend that this is perhaps a more user friendly way to approach it :).

Feel free to use Dapp to play with the look of your app and see how a segmented control might fit with the design.

However... you wanted a leaner approach ;).

I started writing the 10 or so steps needed, but I stopped when I realised you had probably already come to a similar solution. Your string array idea is fine, I assume you will simply convert the slider value to an integer and grab the relevant index from the array?

Sometimes as programmers we go too far with our approaches to a problem. Yes, a string array isn't the most 'flexible' solution but it is fast! And, I'd argue that even a genius mathematical solution isn't as flexible as you may think. Also, if you don't plan on changing the values anytime soon and don't need different slider ranges on different sliders, then it makes sense to just create a static array.

Good luck! :)

这篇关于如何使UISlider输出以指数方式输出漂亮的舍入数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆