I have a slider that I'm using to set a float value somewhere. I connect Value Changed to a method in my viewController. That part works fine.
I need to know when the user starts touching the control but not necessarily every single instant that the slider changes (I receive the Value Changed events for that). So I connected a Touch Up Inside event to another method in the viewController.
The problem it, that method gets called twice when the user touches the UISlider control. WTF? It doesn't work that way with UIButtons or other touch events like Touch Down.
I can work around it, I think, but it seems like a bug in the way the slider control handles touches. Does anybody know why it happens?
BTW: the double touch event happens even when Touch Up Inside is the only connected event.
Just so it doesn't break with future updates
[customSlider addTarget:self action:@selector(sliderDown:) forControlEvents:UIControlEventTouchDown];
[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
- (void)sliderDown:(id)sender
{
sliderused = NO;
}
- (void)sliderAction:(id)sender
{
if(sliderused){
return;
}
sliderused = YES;
//Your Code Here
}
if you are willing to sub-class the slider, you can easily solve this by implementing endTrackingWithTouch and doing nothing:
- (void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{}
the only caveat here is that some specific UIControlEvents will not fire properly. i believe they are the following ones, but i didn't do specific testing to see which ones work and which don't. however touch changed and touch up inside definitely do work without duplicate firing.
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
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