Ios – Detecting touches on a UISlider

cocoa-touchiosobjective cuisliderxcode

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.

Best Answer

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];
Related Topic