Detecting touches on a UISlider?

Andrew Martin picture Andrew Martin · Jun 10, 2012 · Viewed 10.2k times · Source

I have a UISlider on screen, and I need to be able to detect when the user stops touching it. (so I can fade some elements away).

I have tried using:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

but this did not work when ending touches on a slider.

Answer

user529758 picture user529758 · Jun 10, 2012

You can detect when a touch ends using two control events; try

[slider addTarget:self action:@selector(touchEnded:) 
                       forControlEvents:UIControlEventTouchUpInside];

or

[slider addTarget:self action:@selector(touchEnded:) 
                       forControlEvents:UIControlEventTouchUpOutside];

If you want to detect both types of the touchesEnd event, use

[slider addTarget:self action:@selector(touchEnded:) 
   forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];