iPhone : How to detect the end of slider drag?

后端 未结 16 2556
不思量自难忘°
不思量自难忘° 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:36

    You can add an action that takes two parameters, sender and an event, for UIControlEventValueChanged:

    [slider addTarget:self action:@selector(onSliderValChanged:forEvent:) forControlEvents:UIControlEventValueChanged]
    

    Then check the phase of the touch object in your handler:

    - (void)onSliderValChanged:(UISlider*)slider forEvent:(UIEvent*)event {     
        UITouch *touchEvent = [[event allTouches] anyObject];
        switch (touchEvent.phase) {     
            case UITouchPhaseBegan:
                // handle drag began
                break;
            case UITouchPhaseMoved:
                // handle drag moved
                break;
            case UITouchPhaseEnded:
                // handle drag ended
                break;
            default:
                break;
        }
    }
    

    Swift 4 & 5

    slider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
    
    @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
        if let touchEvent = event.allTouches?.first {
            switch touchEvent.phase {
            case .began:
                // handle drag began
            case .moved:
                // handle drag moved
            case .ended:
                // handle drag ended
            default:
                break
            }
        }
    }
    

    Note in Interface Builder when adding an action you also have the option to add both sender and event parameters to the action.

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

    Connect Touch Up Inside and Value Changed

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

    In Swift 4 you can use this function

    1 Frist step:- Adding the target in slider

    self.distanceSlider.addTarget(self, action: #selector(self.sliderDidEndSliding(notification:)), for: ([.touchUpInside,.touchUpOutside]))
    

    2 Second step:- Create a function

    @objc func sliderDidEndSliding(notification: NSNotification)
    {
       print("Hello-->\(distanceSlider.value)")
    }
    
    0 讨论(0)
  • 2020-12-02 08:39

    You can use:

    - (void)addTarget:(id)target action:(SEL)action 
                       forControlEvents:(UIControlEvents)controlEvents  
    

    to detect when the touchDown and touchUp events occur in UISlider

    0 讨论(0)
提交回复
热议问题