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.
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];
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With