Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change Uislider value on tapping at particular point

I want to fire an action upon tapping at a particular point on UISlider by tapping, not by dragging the slider thumb.

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

A snippet of code would be appreciated.

like image 804
Ronak Avatar asked May 16 '12 13:05

Ronak


2 Answers

    UISlider *slider = [[[UISlider alloc] init] autorelease];
…

slider setup

…
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];
}
like image 135
Ronak Avatar answered Nov 16 '22 19:11

Ronak


Try uiview touchesbegan function if v1 is a uiview with plain background behind that place 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 with v1 frame that is if v1 frame x value =50 and i = 5means 10 percent then multiply the percentage value with slider value to change the slider value

like image 33
Nazik Avatar answered Nov 16 '22 19:11

Nazik