限制滑动UISlider超过某一点 [英] Limit sliding UISlider past certain point
问题描述
我们假设有一个 UISlider
,我设置了一个特定的值。然后滑块手柄移动到该值,如预期的那样。
Let's assume a have a UISlider
to which I set a certain value. The slider handle then moves to that value, as expected.
现在我想让用户来回滑动滑块的手柄,但是不要设置 UISlider的值
比我以编程方式分配的更少。
Now I want to let the user slide the handle of the slider back and forth, but to not set the value of the UISlider
to be less, than I assigned to it programmatically.
所以例如我有一个 UISlider
最小值为0,最大值为100,我将其值设置为50.我不希望用户滑动lefter而不是50但是在右侧我想让用户滑动来回处理。
So for example I have a UISlider
with a min value of 0 and a max value of 100 and I set its value to be 50. I don't want the user to slide "lefter" than 50 but on the "right" of it I want to let the user slide the handle back and forth.
如何在Obj-C中实现这种行为?
How do you implement this sort of behaviour in Obj-C?
推荐答案
您应首先确保连续发送更新事件:
You should first make sure the updates event are sent continuously:
mySlider.continuous = YES;
您也可以在故事板/笔尖中执行此操作(由@fDmitry提及,这是默认值IB中的状态。
You can also do that in your storyboard/nib (as mentionned by @fDmitry, this is the default state in IB).
然后你必须将你的滑块链接到.h文件中的 IBAction
(创建通过ctrl-从IB拖动到您的代码的链接),并将其分配给滑块的Value Changed事件。这将创建一个这样的方法:
Then you have to link your slider to an IBAction
in your .h file (create a link by ctrl-dragging from IB to your code), and assign it to the "Value Changed" event of your slider. This will create a method like this:
-(IBAction)sliderValueChanged:(id)sender;
以这种方式实施:
- (IBAction)sliderValueChanged:(id)sender {
float maxValue = 50.0f;
if ([(UISlider*)sender value] > maxValue) {
[(UISlider*)sender setValue:maxValue];
}
}
这篇关于限制滑动UISlider超过某一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!