UISlider touch to set value

You may want a user to be able to touch a UISlider somewhere besides the thumb to set it’s value. You can use a UIGestureRecognizer to help do this for you. I did not author this code but found it someplace, I think StackOverflow (where else really? :) )

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
[slider addGestureRecognizer:gr];
- (void)sliderTapped:(UIGestureRecognizer *)g {
     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];
Related Posts Plugin for WordPress, Blogger..."UISlider touch to set value", 5 out of 5 based on 1 ratings.

Leave a Reply

Your email address will not be published. Required fields are marked *

− 3 = five

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>