1

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...

iHunter
  • 6,205
  • 3
  • 38
  • 56
Lyniker Aoyagui
  • 65
  • 1
  • 12
  • Do you want the slider at empty to represent off and full to represent on? Are you going to let them slide it or just tap? – Kekoa Jan 03 '12 at 18:14
  • 1st question: Exactly. 2nd question: Maybe just tap to go to another state (i think it's easier). – Lyniker Aoyagui Jan 03 '12 at 18:26

3 Answers3

4

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
                 }];
bshirley
  • 8,217
  • 1
  • 37
  • 43
1

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

Community
  • 1
  • 1
owen gerig
  • 6,165
  • 6
  • 52
  • 91
  • Won't it just stretch the scale? I mean it doesn't use integers so your slider would return a range of values between 0 and 1. I think he's looking to make it snap to 0 or 1. – Kekoa Jan 03 '12 at 18:13
  • ya good point, but i would think u could round it up or down and if you did it programmatically it should work. but i havnt tried it and maybe its not possible. however i dont see any other way to accomplish this except to maybe know ahead of time whats needed then load a slider or a switch based on the situation. – owen gerig Jan 03 '12 at 18:16
  • I was just asing about the UISlider object funcionality. Otherwise, to the user interaction, I'll do something like: More than 50% (send command on), less than 50% (send command off). – Lyniker Aoyagui Jan 03 '12 at 18:34
  • also when your doing that set the slider value to 1 or 0 so that it actually looks like its slid the whole way – owen gerig Jan 03 '12 at 18:39
1

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;
}
Community
  • 1
  • 1
Paul Cezanne
  • 8,629
  • 7
  • 59
  • 90