如何使用UIScrollView来更改值,就像UISlider一样? [英] How can I use a UIScrollView to change values, just like a UISlider?

查看:97
本文介绍了如何使用UIScrollView来更改值,就像UISlider一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,UISlider可以很好地改变/选择值,但它看起来并不那么好。我想要实现的更像是水平旋转的表盘。我已经拥有了我需要的所有图形,并且我已经实现了滚动视图来左右滑动我的图像。

So a UISlider works great to change/choose values but it just doesn't look that great. What i'd like to implement is more like a horizontally spinning dial. I already have all the graphics I need and i've already implemented the scroll view to slide my image left and right.

此时的问题是unlick in a UISlider,我可以简单地调用 slider.value 来获取它的值,我不知道如何设置一个值范围(最大值和最小值)以及如何获取特定值在UIScrollView中滚动。

The problem at this point is that unlick in a UISlider where I can simply call slider.value to get its value, I have no clue how to set a range of values (max and min) and how to get the specific value out of a UIScrollView as it's scrolling.

推荐答案

我不得不承认你不是100%确定如何使用scrollview作为滑块,但你可以使用获取内容视图的原点偏离滚动视图原点的点的contentOffset属性。

I have to admit to not being 100% sure how you're using a scrollview as a slider, but you can use the contentOffset property to get "The point at which the origin of the content view is offset from the origin of the scroll view."

你需要做一些数学运算才能把它变成你想要的值,但这很简单。假设您的滚动视图是水平的,并且可以滚动到400px。也就是说,如果contentOffset为0,那么你就在开始时,如果contentOffset是400px,那么你就在最后。你如何使用scrollview将决定你如何解决这个问题,如果你绝望,那么你可以通过编程方式打印contentOffset的值,并根据经验观察范围。

You'll need to do some maths to turn that into the value you want, but it's simple stuff. Suppose your scroll view is horizontal and has can be scrolled up to 400px across. That is, if contentOffset is 0 then you're at the start and if contentOffset is 400px then you're at the end. How you've used the scrollview will dictate exactly how you work this out, if you're desperate then you can just print the value of contentOffset programmatically and observe the range empirically.

在这种情况下,你可以得到你所在的滚动视图的距离比例:

In that case, you can get the proportion of the distance across the scrollview you are at by something like:

CGFloat proportionOffset = scrollView.contentOffset/400.0f;

如果你想建模范围[startX,endX]那么只需要解决:

If you then want to model the range [startX, endX] then just work out:

CGFloat offset = startX + proportionOffset * (endX - startX);

proportionOffset在0到1的范围内结束,当contentOffset为0时为0,当contentOffset为1时例如,如果startX为15且endX为35,则endX - startX为20.如果将0到1范围内的数字乘以20,则得到0到20范围内的数字。这是多远你想要的范围。但是你的范围不一定从0开始,所以你需要加上startX的值。

proportionOffset ends up in the range 0 to 1, at 0 when contentOffset is 0 and at 1 when contentOffset is 400. If, say, startX is 15 and endX is 35 then endX - startX is 20. If you multiply a number in the range 0 to 1 by 20 then you get a number in the range 0 to 20. That's how far through your range you want to be. But your range doesn't necessarily start at 0, so you need to add on the value of startX.

为了提高效率,将自己设置为代表是明智之举只有在收到scrollViewDidScroll:时,滚动视图才会检查新的contentOffset。否则你就会进行大量的轮询,而这种轮询根本就没有必要,而且在绝对最糟糕的情况下会严重阻碍CPU的睡眠能力(从而减少热量并节省电量)。

For efficiency terms, it's smart to set yourself up as a delegate of the scrollview and check the new contentOffset only when you receive a scrollViewDidScroll:. Otherwise you're doing a lot of polling that simply isn't necessary and, at the absolute worst, severely impeding the CPU's ability to sleep (and hence reduce heat and save battery).

这篇关于如何使用UIScrollView来更改值,就像UISlider一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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