Detect double-tap on UISlider?

╄→尐↘猪︶ㄣ 提交于 2020-01-23 17:51:45

问题


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


回答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; 
    }
  }
}



回答2:


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");
}



回答3:


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



来源:https://stackoverflow.com/questions/1392550/detect-double-tap-on-uislider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!