iPhone : How to detect the end of slider drag?

后端 未结 16 2557
不思量自难忘°
不思量自难忘° 2020-12-02 07:36

How to detect the event when the user has ended the drag of a slider pointer?

相关标签:
16条回答
  • 2020-12-02 08:25

    Swift 3 answer

    I had same problem and eventually got this to work, so maybe it will help somebody. Connect your_Slider to 'Value Changed' in storyboard and when slider moved "Slider Touched" actioned and when let go "Slider Released".

    @IBAction func your_Slider(_ sender: UISlider) {
        if (yourSlider.isTracking) {
            print("Slider Touched")
        } else {
            print("Slider Released")
        }
    }
    
    0 讨论(0)
  • 2020-12-02 08:26

    Swift 3.1

    Sometimes you will want the slider's drag events to fire continuously, but have the option of executing different code depending on whether the slider is still being dragged or has just finished being dragged.

    To do this, I've expanded on Andy's answer above that makes use of the slider's isTracking property. I needed to record two states: sliderHasFinishedTracking and sliderLastStoredValue.

    My code correctly:

    • doesn't fire an action upon starting drag

    • fires the action if the user only nudges the slider with a single tap (meaning that isTracking remains false)


    class SliderExample: UIViewController {
      var slider: UISlider = UISlider()
      var sliderHasFinishedTracking: Bool = true
      var sliderLastStoredValue: Float!
    
      override func loadView() {
        super.loadView()
    
        slider.minimumValue = 100.0
        slider.maximumValue = 200.0
        slider.isContinuous = true
        slider.value = 100.0
        self.sliderLastStoredValue = slider.value
        slider.addTarget(self, action: #selector(respondToSlideEvents), for: .valueChanged)
        // add your slider to the view however you like
      }
    
      func respondToSlideEvents(sender: UISlider) {
        let currentValue: Float = Float(sender.value)
        print("Event fired. Current value for slider: \(currentValue)%.")
    
        if(slider.isTracking) {
          self.sliderHasFinishedTracking = false
          print("Action while the slider is being dragged ⏳")
        } else {
          if(self.sliderHasFinishedTracking && currentValue == self.sliderLastStoredValue){
            print("Slider has finished tracking and its value hasn't changed, " +
                  "yet event was fired anyway.                                                                     
    0 讨论(0)
  • 2020-12-02 08:27

    If you don't need any data inbetween drag, than you should simply set:

    [mySlider setContinuous: NO];
    

    This way you will receive valueChanged event only when the user stops moving the slider.

    Swift 5 version:

    mySlider.isContinuous = false
    
    0 讨论(0)
  • 2020-12-02 08:32

    I use the "Touch Up Inside" and "Touch up outside" notifications.

    Interface Builder:

    Connect both notifications in the Interface Builder to your receiving method. The method could look like this:

    - (IBAction)lengthSliderDidEndSliding:(id)sender {
        NSLog(@"Slider did end sliding... Do your stuff here");
    }
    

    In code:

    If you want to wire it programatically you would have something like this in your viewWillAppear (or wherever it fits you) call:

    [_mySlider addTarget:self
                  action:@selector(sliderDidEndSliding:) 
        forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
    

    The receiving method would look like this:

    - (void)sliderDidEndSliding:(NSNotification *)notification {
         NSLog(@"Slider did end sliding... Do your stuff here");
    }
    
    0 讨论(0)
  • 2020-12-02 08:32

    I think you need the control event UIControlEventTouchUpInside.

    0 讨论(0)
  • 2020-12-02 08:35

    Try like this

    customSlider.minimumValue = 0.0;
        customSlider.maximumValue = 10.0;
    [customSlider addTarget:self action:@selector(changeSlider:) forControlEvents:UIControlEventValueChanged];
    
    
    -(void)changeSlider:(id)sender{
        UISlider *slider=(UISlider *)sender;
        float sliderValue=(float)(slider.value );
    NSLog(@"sliderValue = %f",sliderValue);
    }
    
    0 讨论(0)
提交回复
热议问题