views:

424

answers:

2

I can detect single/double-taps in specific views with:

NSSet *myTouches = [event touchesForView:mySpecificView.view];

but I want to detect a double-tap on the button of a slider and can't find any reference to it. Is there a replacement for "touchesForView:" where I can enter the name of my slider?

usage: I have three sliders with their default value being directly in the center of the slider. Once the position of the slider has changed, I want a quick way to individually reset each slider to its default position.

I currently have each slider's containing view set to respond to a double-tap, updating each slider. It works fine, but doesn't seem natural. ie.I can't double-tap on the slider itself because the slider intercepts the taps and doesn't pass them on to the surrounding view.

thanks in advance

A: 

I was looking for a way to pass touch events from a UISlider to a UITableView when I came across your question.

If you haven't figured it out by now, or for anyone else with a similar question, you can detect a double tap on a UISlider object by setting an action method to it like so:

- (void)loadView {

   UISlider *slider = [[UISlider alloc] initWithFrame:sliderBackground.frame];
   [view addSubview:slider];

   // Set the slider action method
   [slider addTarget:self 
           action:@selector(sliderDoubleTap:) 
           forControlEvents:UIControlEventTouchDownRepeat];

}


- (void) sliderDoubleTap: (UISlider *) sender {
    NSLog(@"Slider Double Tap");
}
Thanks for the suggestion user211790, but that didn't quite do it. I was already able to drag from the slider's TouchDownRepeat event to a method in Interface Builder, which allowed me to set both the slider and the slider's label (a UILabel) to "100". Problem was, as soon as I released that second tap, the slider would jump back to wherever my finger had double tapped. ie."50" etc. I was able to figure it out with two methods, see below.
Randy
A: 

By utilizing two methods, connecting the TouchDownRepeat event of a slider to one (doubleTapSlider) and TouchUpInside event of the same slider to another (releaseSliderBtn), I was able to get the exact functionality I was looking for. I needed this for three separate sliders, so I assigned them tags of 5, 6 & 7 in Interface Builder to detect which one was calling my methods.

User double-taps a slider ball, which moves the ball back to the center and sets the label to 100. If I didn't have the releaseSliderBtn method, when the user released the second tap, the label would stay at 100, but the ball would jump back to where the user released the second tap. Now, by checking the label's current text, when the user releases the second tap, the ball remains at the center location. Slider 1's range is 50~150. Slider 2's range is -12~+12. Slider 3's range is -49~+49.

FYI: For some reason, 'else if' wasn't working for releaseSliderBtn, so that's why I made them three separate if statements.

-(IBAction)doubleTapSlider:(id)sender {
  printf ("Sender: %d\n", [sender tag]);
  if([sender tag] == 5){
    playbackSpeedSlider.value = 100;
    playbackSpeedLabel.text = @"100";
  } else if ([sender tag] == 6) {
    halfStepSlider.value = 0;
    halfStepLabel.text = @"0";
  } else if ([sender tag] == 7) {
    centsSlider.value = 0;
    centsLabel.text = @"0";
  }
}
-(IBAction)releaseSliderBtn:(id)sender {
  printf ("Sender: %d\n", [sender tag]);
  if([sender tag] == 5){
    if(playbackSpeedLabel.text == @"100"){
      playbackSpeedSlider.value = 100;
    }
  }
  if ([sender tag] == 6) {
    if(halfStepLabel.text == @"0"){
      halfStepSlider.value = 0;
    }
  }
  if ([sender tag] == 7) {
    if(centsLabel.text == @"0"){
      centsSlider.value = 0; 
    }
  }
}
Randy