Change Uislider value when clicking on a specific point

I want to trigger an action when I click on a specific point on the UISlider by clicking rather than dragging a large slider pointer.

How can I click on UISlider at a point to set the value of the slider?

A piece of code will be appreciated.

+5
source share
2 answers
    UISlider *slider = [[[UISlider alloc] init] autorelease];

slider setting

UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)] autorelease];
[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];
}
+36
source

Try activating the uiview function if v1 is a uiview with a simple background behind this uislider \

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  if ([touch view] == v1)
  {
    CGPoint location = [touch locationInView:v1];
    NSInteger i = location.x;
  }

then get the percentage of xpoint by multiplying it by the v1 frame, if the v1 frame x = 50 and i = 5 means 10 percent, then multiply the percentage value with the value of the slider to change the value of the slider

+2
source

All Articles