Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISlider returns two Touch Up Inside events, why does that happen?

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.

like image 342
willc2 Avatar asked Jun 30 '09 11:06

willc2


2 Answers

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
}
like image 101
bentech Avatar answered Sep 30 '22 16:09

bentech


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,
like image 39
jason Avatar answered Sep 30 '22 18:09

jason