注意:我使用的是Monotouch。
我想知道是否有一种方法可以将UISlider对象用作UISwitch对象。我是说..。我只希望在两种状态(100%或0%)下使用UISlider对象,并且希望阻止其他状态进行用户交互。
我尝试这样做是因为我正在开发一个软件,它将控制一些模块的照明。问题是有调光器(0%到100%)的模块和非调光器模块(0%或100%的值),我需要UISlider像我之前提到的第二种情况那样工作。
UISwitch的问题是在视觉上是一个完全不同的组件,它不像滑块那么薄,我不能把代表开和关状态的图标放在旁边(就像附件中的照片一样),我想知道UI可能会被滑块和开关弄得一团糟……

发布于 2012-01-04 02:38:29
您可以设置UISlider的目标/操作,并以编程方式将其强制到其中一个极端。
我将continuous设置为NO,然后将滑块设置为动画到更近的一端(以便让用户更清楚地看到正在发生的事情)。
编辑:根据你的问题,这里有一个典型的机制来动画值:
UISlider *slider;
[UIView animateWithDuration:0.2 // seconds
animations:^{
slider.value = (YES) ? 1.0 : 0.0; // modify this line as appropriate
}];发布于 2012-01-04 02:07:55
如果使用IB,则可以设置滑块的最小值和最大值,因此请分别使用0和1。或者你也可以用代码来实现,如下所示:
look here
或者从那个帖子上复制和粘贴:
UISlider* slider = [[UISlider alloc] init];
slider.minimumValue = -3.0f;
slider.maximumValue = 3.0f;它是一个浮点值,因此是3.0f loat
发布于 2012-01-04 03:40:28
我认为您希望滑块对齐到0或1。使用代码找到的in this stackoverflow question并适当设置您的值。有趣的巧合,不到10分钟前,我刚刚完成了对抓取滑块的编码。为了节省点击,这里是你想要的代码,从该问题中复制/粘贴:
-(IBAction)valueChanged:(id)sender {
// This determines which "step" the slider should be on. Here we're taking
// the current position of the slider and dividing by the `self.stepValue`
// to determine approximately which step we are on. Then we round to get to
// find which step we are closest to.
float newStep = roundf((questionSlider.value) / self.stepValue);
// Convert "steps" back to the context of the sliders values.
self.questionSlider.value = newStep * self.stepValue;
}https://stackoverflow.com/questions/8716652
复制相似问题