1

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

Randy
  • 61
  • 9
  • *bump* Either this is something no one has done/wants to do, or it's so simple everyone assumes I'll stumble across it. Either way, I'm still stuck. Anyone? – Randy Sep 21 '09 at 16:05

3 Answers3

1

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
  • 61
  • 9
0

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");
}
Dan Sandland
  • 7,095
  • 2
  • 29
  • 29
  • 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 May 02 '10 at 21:07
0

Updated for Swift 3.0

This is an old question that I stumbled across while solving an almost identical problem of adding a double tap recognizer to reset a slider.

The original answer points us in the right direction by adding an action to respond to UIControlEvents.touchDownRepeat.

This can be done in swift by adding the line in your view controller (for example in the viewDidLoad() method:

override func viewDidLoad() 
{     
    super.viewDidLoad()
    self.slider.addTarget(self, action: #selector(sliderTouchDownRepeat(_ :)), for: .touchDownRepeat)
}

And you can respond accordingly in the selector:

@objc func sliderTouchDownRepeat(_ slider: UISlider) 
{       
        //Respond to double tap here
}

This is all well and good, although there are some additional pieces of code that are necessary if you plan on changing the slider value when the double tap is recognized.

First, also add an action for the UIControlEvents.valueChanged

override func viewDidLoad() 
{     
    super.viewDidLoad()
    self.slider.addTarget(self, action: #selector(sliderTouchDownRepeat(_ :)), for: .touchDownRepeat)
    self.slider.addTarget(self, action: #selector(sliderDidChange(_:)), for: .valueChanged)
}

Cancel the current touch down event that the we are responding to so that the slider thumb is not immediately returned to the double tap location slider.cancelTracking(with: nil) and call the action for the UIControlEvents.valueChanged

@objc func sliderTouchDownRepeat(_ slider: UISlider) 
{
    slider.cancelTracking(with: nil)

    // Perform your own value change operation
    // self.reset()

    self.sliderDidChange(slider)
}

@objc  func sliderDidChange(_ slider: UISlider) 
{
    //Update any other UI here
}

Happy coding

Glavid
  • 1,071
  • 9
  • 19