使用 UISlider 作为 UISwitch (两个状态对象)

发布于 2024-12-23 17:53:27 字数 471 浏览 1 评论 0原文

注意:我使用的是 Monotouch。

我想知道是否有办法将 UISlider 对象用作 UISwitch 对象。我的意思是...我只想在两种状态(100% 或 0%)下使用 UISlider 对象,并且我想阻止用户交互的其他状态。

我尝试这样做是因为我正在开发一个软件,它将控制某些模块的照明。问题是有些模块带有调光器(0% 到 100%),也有非调光器模块(0% 或 100% 值),并且我需要 UISlider 工作,正如我之前在第二种情况。

UISwitch 的问题是,它在视觉上是一个完全不同的组件,它不像滑块那么薄,而且我无法放置代表 ON 和 OFF 状态的图标(在它旁边,如附图所示),我想知道也许 UI 会因为滑块和开关而变得混乱...

Note: I'm using Monotouch.

I'd like to know if there's a way to use UISlider object as an UISwitch object. I mean... I want to use the UISlider object only in two states (100% or 0%), and I want to block the other states for the user interaction.

I'm trying to do this because I'm developing a software, and it'll control the illumination of some modules. The problem is that there are modules with dimmer (0% through 100%), and non-dimmer modules (0% or 100% value), and I need the UISlider working as I mentioned before in the second case.

The problem with the UISwitch is that is a totally different component visually, it's not as thin as the slider and i cannot put icons representing the state of ON and OFF (beside it as in the attached photo), and I was wondering that maybe the UI is gonna be a mess with slider and switchs...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-12-30 17:53:27

您可以设置 UISlider 的目标/操作,并以编程方式强制其达到极端之一。

我将连续 设置为NO,然后将滑块设置为接近末端的动画(以使用户更清楚地了解正在发生的情况)。

编辑:
根据您的问题,这是一种为值设置动画的典型机制:

UISlider *slider;
[UIView animateWithDuration:0.2 // seconds
                 animations:^{
                   slider.value = (YES) ? 1.0 : 0.0; // modify this line as appropriate
                 }];

You can set the target/action of the UISlider and programmatically force it to one of the extremes.

I'd set continuous to NO, and then animate the slider to the closer end (to make it more apparent to the user what is happening).

Edit:
per your question, here's a typical mechanism to animate a value:

UISlider *slider;
[UIView animateWithDuration:0.2 // seconds
                 animations:^{
                   slider.value = (YES) ? 1.0 : 0.0; // modify this line as appropriate
                 }];
你穿错了嫁妆 2024-12-30 17:53:27

如果您使用 IB,您可以设置滑块的最小值和最大值,因此分别使用 0、1。或者您可以在代码中执行此操作,请参阅下文:

查看此处

或从该帖子复制并粘贴:

UISlider* slider = [[UISlider alloc] init];
slider.minimumValue = -3.0f;
slider.maximumValue = 3.0f;

它是一个浮点值,因此是 3.0f loat

if you use IB you can set the min and max values for a slider, so use 0, 1 respectively. or you can do it in code, see below:

look here

or coppied and pasted from that post:

UISlider* slider = [[UISlider alloc] init];
slider.minimumValue = -3.0f;
slider.maximumValue = 3.0f;

its a float value hence the 3.0f loat

拿命拼未来 2024-12-30 17:53:27

我认为您希望滑块捕捉到 0 或 1。使用在此 stackoverflow 问题 并适当地设置您的值。有趣的是,不到 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;
}

I think you want your slider to snap to 0 or 1. Use the code found in this stackoverflow question and set your values appropriately. Funny coincidence, I just finished coding this upself for a snapping slider not 10 minutes ago. To save clicking, here's the code you want, copy/pasted from that question:

-(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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文