10

I'm developing my first iOS app which contains a UISlider. I know how to get the value when the UISlider is dragged. But for my app I need to get the slider's value in a single touch; i.e. if I touch somewhere in the UISlider, a UILabel should display its correct value.

Is it possible to this way. Any tutorial or code will very helpful.

4 Answers 4

20

Here tapCount is int variable that declare in .h file

- (void)viewDidLoad
{
  tapCount = 0; // Count tap on Slider 

 UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
    [slider addGestureRecognizer:gr];
    
}

 - (void)sliderTapped:(UIGestureRecognizer *)g 
    {
/////////////// For TapCount////////////

        tapCount = tapCount + 1;
        NSLog(@"Tap Count -- %d",tapCount);

/////////////// For TapCount////////////

        UISlider* s = (UISlider*)g.view;
        if (s.highlighted)
            return; // tap on thumb, let slider deal with it
        CGPoint pt = [g locationInView: s];
        CGFloat percentage = pt.x / s.bounds.size.width;
        CGFloat delta = percentage * (s.maximumValue - s.minimumValue);
        CGFloat value = s.minimumValue + delta;
        [s setValue:value animated:YES];
        
        NSString *str=[NSString stringWithFormat:@"%.f",[self.slider value]];
        self.lbl.text=str;
    }

For Swift User

override func viewDidLoad()
{
    super.viewDidLoad()

    tapCount = 0
    let gr = UITapGestureRecognizer(target: self, action: #selector(self.sliderTapped(_:)))
    slider.addGestureRecognizer(gr)

}

func sliderTapped(_ g: UIGestureRecognizer) {
    
    /////////////// For TapCount////////////
    tapCount = tapCount + 1
    print("Tap Count -- \(tapCount)")
    /////////////// For TapCount////////////
    
    let s: UISlider? = (g.view as? UISlider)
    if (s?.isHighlighted)! {
        return
    }
    
    // tap on thumb, let slider deal with it
    let pt: CGPoint = g.location(in: s)
    let percentage = pt.x / (s?.bounds.size.width)!
    let delta = Float(percentage) * Float((s?.maximumValue)! - (s?.minimumValue)!)
    let value = (s?.minimumValue)! + delta
    s?.setValue(Float(value), animated: true)
    let str = String(format: "%.f", slider.value)
    lbl.text = str
}
0
10

Correction @ipatel: Math in previous answers is incomplete and causes jitter due to missing thumb width considerations.

Here is what it should look like if you choose to subclass UISlider:

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super beginTrackingWithTouch:touch withEvent:event];
    CGPoint touchLocation = [touch locationInView:self];
    CGFloat value = self.minimumValue + (self.maximumValue - self.minimumValue) *
        ((touchLocation.x - self.currentThumbImage.size.width/2) /
        (self.frame.size.width-self.currentThumbImage.size.width));
    [self setValue:value animated:YES];

    return YES;
}
1
  • In the long run, I found that overwriting UISlider was brittle, and needed constant maintenance to keep up with iOS upgrades. I better solution is published at github.com/arquebuse/TGPControls. It does not override UISlider, and thus has proven much more robust. It also uses CALayer for animations, which are smooth and entirely customizable. Jun 30, 2015 at 23:29
2

Swift version for accepted answer,

@IBAction func sliderTappedAction(sender: UITapGestureRecognizer)
{
    if let slider = sender.view as? UISlider {

        if slider.highlighted { return }

        let point = sender.locationInView(slider)
        let percentage = Float(point.x / CGRectGetWidth(slider.bounds))
        let delta = percentage * (slider.maximumValue - slider.minimumValue)
        let value = slider.minimumValue + delta
        slider.setValue(value, animated: true)
    }
}
0

You can display the current value of UISlider with its action method like below.

- (IBAction) sliderValueChanged:(UISlider *)sender {  
    yourLable.text = [NSString stringWithFormat:@"%.1f", [sender value]];  
} 

See these tutorials and examples of UISlider:

  1. UISlider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode.
  2. iphone-slider-control-uislider-control-tutorial.

This second tutorial with example which display the current (Latest) value of UISlider in UILable see the image below for example.

enter image description here

3
  • Thanx for your help Paras. But I wasnt looking for this. I ot the answer im looking for from another question here. This was what i am looking for stackoverflow.com/questions/2895122/…
    – Midas
    Jan 16, 2013 at 11:00
  • then what you want exactly ?? Jan 16, 2013 at 11:00
  • @Ravisankarvm oh ok ok now i understand that use click or tap on screen just give that value with changed slider value... Jan 16, 2013 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.